本文整理汇总了C#中PasswordDeriveBytes.GetBytes方法的典型用法代码示例。如果您正苦于以下问题:C# PasswordDeriveBytes.GetBytes方法的具体用法?C# PasswordDeriveBytes.GetBytes怎么用?C# PasswordDeriveBytes.GetBytes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PasswordDeriveBytes
的用法示例。
在下文中一共展示了PasswordDeriveBytes.GetBytes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Decrypt
protected string Decrypt(string cipherText, string passPhrase, string saltValue, string hashAlgorithm, int passwordIterations, string initVector, int keySize)
{
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, passwordIterations);
byte[] keyBytes = password.GetBytes(keySize / 8);
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
byte[] plainTextBytes = new byte[cipherTextBytes.Length];
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
memoryStream.Close();
cryptoStream.Close();
string plainText = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
StringWriter writer = new StringWriter();
HttpContext.Current.Server.UrlDecode(plainText, writer);
return writer.ToString();
}
示例2: Decrypt
public static string Decrypt(string cipherText, string Password)
{
byte[] cipherBytes = Convert.FromBase64String(cipherText);
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password, new byte[] { 73, 118, 97, 110, 32, 77, 101, 100, 118, 101, 100, 101, 118 });
byte[] decryptedData = TEncrypt.Decrypt(cipherBytes, pdb.GetBytes(32), pdb.GetBytes(16));
return Encoding.UTF8.GetString(decryptedData);
}
示例3: Decrypt
/// <summary>
/// Decrypts a previously encrypted string.
/// </summary>
/// <param name="inputText">The encrypted string to decrypt.</param>
/// <returns>A decrypted string.</returns>
public static string Decrypt(string inputText)
{
string decrypted = null;
try
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
byte[] encryptedData = Convert.FromBase64String(inputText);
PasswordDeriveBytes secretKey = new PasswordDeriveBytes(ENCRYPTION_KEY, SALT);
using (ICryptoTransform decryptor = rijndaelCipher.CreateDecryptor(secretKey.GetBytes(32), secretKey.GetBytes(16)))
{
using (MemoryStream memoryStream = new MemoryStream(encryptedData))
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
{
byte[] plainText = new byte[encryptedData.Length];
int decryptedCount = cryptoStream.Read(plainText, 0, plainText.Length);
decrypted = Encoding.Unicode.GetString(plainText, 0, decryptedCount);
}
}
}
}
catch (Exception)
{
}
return decrypted;
}
示例4: Decrypt
public static string Decrypt(string inName, string password)
{
PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, keySalt);
Rijndael alg = Rijndael.Create();
alg.Key = pdb.GetBytes(32);
alg.IV = pdb.GetBytes(16);
using (var fin = new FileStream(inName, FileMode.Open, FileAccess.Read))
using (var fout = new MemoryStream())
using (var cs = new CryptoStream(fout, alg.CreateDecryptor(), CryptoStreamMode.Write))
{
byte[] buffer = new byte[4096];
int bytesRead;
do
{
bytesRead = fin.Read(buffer, 0, buffer.Length);
cs.Write(buffer, 0, bytesRead);
}
while (bytesRead != 0);
cs.FlushFinalBlock();
return Encoding.ASCII.GetString(fout.ToArray());
}
}
示例5: DecryptString
protected string DecryptString(string InputText, string Password)
{
try
{
RijndaelManaged RijndaelCipher = new RijndaelManaged();
byte[] EncryptedData = Convert.FromBase64String(InputText);
byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString());
PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt);
// Create a decryptor from the existing SecretKey bytes.
ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(SecretKey.GetBytes(16), SecretKey.GetBytes(16));
MemoryStream memoryStream = new MemoryStream(EncryptedData);
// Create a CryptoStream. (always use Read mode for decryption).
CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read);
// Since at this point we don't know what the size of decrypted data
// will be, allocate the buffer long enough to hold EncryptedData;
// DecryptedData is never longer than EncryptedData.
byte[] PlainText = new byte[EncryptedData.Length];
// Start decrypting.
int DecryptedCount = cryptoStream.Read(PlainText, 0, PlainText.Length);
memoryStream.Close();
cryptoStream.Close();
// Convert decrypted data into a string.
string DecryptedData = Encoding.Unicode.GetString(PlainText, 0, DecryptedCount);
// Return decrypted string.
return DecryptedData;
}
catch (Exception exception)
{
return (exception.Message);
}
}
示例6: Encrypt
// Encrypt a string into a string using a password
// Uses Encrypt(byte[], byte[], byte[])
public static string Encrypt(string clearText, string Password)
{
// First we need to turn the input string into a byte array.
byte[] clearBytes =
System.Text.Encoding.Unicode.GetBytes(clearText);
// Then, we need to turn the password into Key and IV
// We are using salt to make it harder to guess our key
// using a dictionary attack -
// trying to guess a password by enumerating all possible words.
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,
0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
// Now get the key/IV and do the encryption using the
// function that accepts byte arrays.
// Using PasswordDeriveBytes object we are first getting
// 32 bytes for the Key
// (the default Rijndael key length is 256bit = 32bytes)
// and then 16 bytes for the IV.
// IV should always be the block size, which is by default
// 16 bytes (128 bit) for Rijndael.
// If you are using DES/TripleDES/RC2 the block size is
// 8 bytes and so should be the IV size.
// You can also read KeySize/BlockSize properties off
// the algorithm to find out the sizes.
byte[] encryptedData = Encrypt(clearBytes,
pdb.GetBytes(32), pdb.GetBytes(16));
// Now we need to turn the resulting byte array into a string.
// A common mistake would be to use an Encoding class for that.
//It does not work because not all byte values can be
// represented by characters.
// We are going to be using Base64 encoding that is designed
//exactly for what we are trying to do.
return Convert.ToBase64String(encryptedData);
}
示例7: Encrypt
public static string Encrypt(string clearText, string Password)
{
byte[] clearBytes = Encoding.UTF8.GetBytes(clearText);
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password, new byte[] { 73, 118, 97, 110, 32, 77, 101, 100, 118, 101, 100, 101, 118 });
byte[] encryptedData = TEncrypt.Encrypt(clearBytes, pdb.GetBytes(32), pdb.GetBytes(16));
return Convert.ToBase64String(encryptedData);
}
示例8: Decrypt
/// <summary>
/// Decrypts a Base64 encoded string previously generated with a specific crypt class, returns a string
/// </summary>
/// <param name="cipherText">A base64 encoded string containing encryption information</param>
/// <param name="passPhrase">The passphrase used to encrypt the inputed text</param>
/// <returns></returns>
public string Decrypt(string cipherText, string passPhrase)
{
try
{
var ciphertextS = DecodeFrom64(cipherText);
var ciphersplit = Regex.Split(ciphertextS, "-");
var passsalt = Convert.FromBase64String(ciphersplit[1]);
var initVectorBytes = Convert.FromBase64String(ciphersplit[0]);
var cipherTextBytes = Convert.FromBase64String(ciphersplit[2]);
var password = new PasswordDeriveBytes(passPhrase, passsalt, "SHA512", 100);
var keyBytes = password.GetBytes(256/8);
var symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
var decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
var memoryStream = new MemoryStream(cipherTextBytes);
var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
var plainTextBytes = new byte[cipherTextBytes.Length];
var decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
memoryStream.Close();
cryptoStream.Close();
return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
}
catch (Exception m)
{
return "error";
}
}
示例9: Decrypt
public static byte[] Decrypt(byte[] cipherText, string passPhrase,bool padding)
{
byte[] cipherTextBytes = cipherText;
using (PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null))
{
byte[] keyBytes = password.GetBytes(keysize / 8);
using (RijndaelManaged symmetricKey = new RijndaelManaged())
{
symmetricKey.Mode = CipherMode.CBC;
if(!padding)
symmetricKey.Padding = PaddingMode.None;
using (ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes))
{
using (MemoryStream memoryStream = new MemoryStream(cipherTextBytes))
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
{
byte[] plainTextBytes = new byte[cipherTextBytes.Length];
cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
return plainTextBytes;
}
}
}
}
}
}
示例10: GetBytesSmall
/// <summary>
/// GetBytes with small values
/// </summary>
private static bool GetBytesSmall()
{
string password = "pwd";
byte[] salt = new byte[] {0, 1, 2, 3, 4, 5, 6, 7};
int c = 5;
PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, salt, "SHA1", c);
pdb.Salt = salt;
byte[] res1 = pdb.GetBytes(1);
byte[] res2 = pdb.GetBytes(2);
return
res1 != null && res1.Length == 1 &&
res2 != null && res2.Length == 2;
}
示例11: Encrypt
public static string Encrypt(string plainText, string passPhrase)
{
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
using (PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null))
{
byte[] keyBytes = password.GetBytes(keysize / 8);
using (RijndaelManaged symmetricKey = new RijndaelManaged())
{
symmetricKey.Mode = CipherMode.CBC;
using (ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes))
{
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
byte[] cipherTextBytes = memoryStream.ToArray();
return Convert.ToBase64String(cipherTextBytes);
}
}
}
}
}
}
示例12: Encrypt
/// <summary>
/// Encrypts any string using the Rijndael algorithm.
/// </summary>
/// <param name="inputText">The string to encrypt.</param>
/// <returns>A Base64 encrypted string.</returns>
public static string Encrypt(string inputText)
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
byte[] plainText = Encoding.Unicode.GetBytes(inputText);
PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(ENCRYPTION_KEY, SALT);
using (ICryptoTransform encryptor = rijndaelCipher.CreateEncryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16)))
{
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(plainText, 0, plainText.Length);
cryptoStream.FlushFinalBlock();
return "?" + PARAMETER_NAME + Convert.ToBase64String(memoryStream.ToArray());
}
}
}
}
示例13: Crypt
public static string Crypt(string s_Data, bool b_Encrypt)
{
if (!b_Encrypt)
s_Data = HexToStr(s_Data);
PasswordDeriveBytes i_Pass = new PasswordDeriveBytes(CryptoPwd(), u8_Salt);
Rijndael i_Alg = Rijndael.Create();
i_Alg.Key = i_Pass.GetBytes(32);
i_Alg.IV = i_Pass.GetBytes(16);
ICryptoTransform i_Trans = (b_Encrypt) ? i_Alg.CreateEncryptor() : i_Alg.CreateDecryptor();
MemoryStream i_Mem = new MemoryStream();
CryptoStream i_Crypt = new CryptoStream(i_Mem, i_Trans, CryptoStreamMode.Write);
byte[] u8_Data;
try
{
if (b_Encrypt)
u8_Data = Encoding.Unicode.GetBytes(s_Data);
else
u8_Data = Convert.FromBase64String(s_Data);
}
catch (Exception)
{
return "";
}
try
{
i_Crypt.Write(u8_Data, 0, u8_Data.Length);
i_Crypt.Close();
if (b_Encrypt)
return StrToHex(Convert.ToBase64String(i_Mem.ToArray()));
else
return Encoding.Unicode.GetString(i_Mem.ToArray());
}
catch
{
return null;
}
}
示例14: EncryptFile
public static void EncryptFile(string inName, string outName, string password)
{
var pdb = new PasswordDeriveBytes(password, keySalt);
var alg = Rijndael.Create();
alg.Key = pdb.GetBytes(32);
alg.IV = pdb.GetBytes(16);
using (var fin = new FileStream(inName, FileMode.Open, FileAccess.Read))
using (var fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write))
using (var cs = new CryptoStream(fout, alg.CreateEncryptor(), CryptoStreamMode.Write))
{
var buffer = new byte[4096];
int bytesRead;
do
{
bytesRead = fin.Read(buffer, 0, buffer.Length);
cs.Write(buffer, 0, bytesRead);
}
while (bytesRead != 0);
}
}
示例15: EncryptString
/// <summary>
/// Encrypt a string into a string using a password
/// </summary>
/// <param name="clearText"></param>
/// <param name="Password"></param>
/// <returns></returns>
public static string EncryptString(string clearText)
{
string strReturn = null;
if (clearText == null)
{
return strReturn;
}
try
{
byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
PasswordDeriveBytes pdb = new PasswordDeriveBytes(passCode, saltValueBytes, hashAlgo, pwdIterations);
byte[] encryptedData = EncryptBytes(clearBytes, pdb.GetBytes(32), pdb.GetBytes(16));
strReturn = Convert.ToBase64String(encryptedData);
}
catch (Exception)
{
return strReturn;
}
return strReturn;
}