本文整理匯總了C#中System.Security.Cryptography.Rfc2898DeriveBytes.Reset方法的典型用法代碼示例。如果您正苦於以下問題:C# Rfc2898DeriveBytes.Reset方法的具體用法?C# Rfc2898DeriveBytes.Reset怎麽用?C# Rfc2898DeriveBytes.Reset使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Security.Cryptography.Rfc2898DeriveBytes
的用法示例。
在下文中一共展示了Rfc2898DeriveBytes.Reset方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: ToKeyDevination
/// <summary>
/// Deviate byte data based on supported types.
/// </summary>
/// <param name="data">Data of any encoding type.</param>
/// <param name="salt">Divination salt.</param>
/// <param name="length">Divination returned byte size.</param>
/// <returns>SaltedData</returns>
public static SaltedData ToKeyDevination(this byte[] data, byte[] salt = null, int length = 32)
{
var salting = salt ?? 16.ToRandomBytes();
var pbkdf2 = new Rfc2898DeriveBytes(data, salting, PBKDF2_ITERATIONS);
var derived = pbkdf2.GetBytes(length);
pbkdf2.Reset();
return new SaltedData() { Salt = salting, Data = derived };
}
示例2: SplitCalls_Reset
public void SplitCalls_Reset ()
{
Rfc2898DeriveBytes keygen = new Rfc2898DeriveBytes ("password", salt, 12345);
byte [] big = keygen.GetBytes (48);
keygen.Reset ();
byte [] a = keygen.GetBytes (16);
byte [] b = keygen.GetBytes (32);
CompareBuffers (big, a, b);
}
示例3: RFC3211_TC2_Reset
public void RFC3211_TC2_Reset ()
{
byte[] expected = { 0x6A, 0x89, 0x70, 0xBF, 0x68, 0xC9, 0x2C, 0xAE };
Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("All n-entities must communicate with other n-entities via n-1 entiteeheehees", salt, 500);
byte[] key1 = pkcs5.GetBytes (8);
Assert.AreEqual (expected, key1, "RFC3211_TC2_part1");
pkcs5.Reset ();
byte[] key2 = pkcs5.GetBytes (8);
Assert.AreEqual (expected, key2, "RFC3211_TC2_part2");
}
示例4: MsdnExample
public void MsdnExample()
{
const string pwd1 = "[email protected]!123";
const int myIterations = 1000;
// data1 can be a string or contents of a file.
const string data1 = "Some test data";
// Create a byte array to hold the random value.
byte[] randomSalt = new byte[8];
using (RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider())
{
// Fill the array with a random value.
rngCsp.GetBytes(randomSalt);
}
//// try
//// {
// The default iteration count is 1000 so the two methods use the same iteration count.
Rfc2898DeriveBytes k1 = new Rfc2898DeriveBytes(pwd1, randomSalt, myIterations);
Rfc2898DeriveBytes k2 = new Rfc2898DeriveBytes(pwd1, randomSalt);
// Encrypt the data.
SymmetricAlgorithm encAlg = TripleDES.Create();
encAlg.Key = k1.GetBytes(16);
MemoryStream encryptionStream = new MemoryStream();
CryptoStream encrypt = new CryptoStream(
encryptionStream, encAlg.CreateEncryptor(), CryptoStreamMode.Write);
byte[] utfD1 = new UTF8Encoding(false).GetBytes(data1);
encrypt.Write(utfD1, 0, utfD1.Length);
encrypt.FlushFinalBlock();
encrypt.Close();
byte[] edata1 = encryptionStream.ToArray();
// Erase memory for optimum secrecy?
k1.Reset();
// Try to decrypt, thus showing it can be round-tripped.
SymmetricAlgorithm decAlg = TripleDES.Create();
decAlg.Key = k2.GetBytes(16);
// Relay the one-time INITIALIZATION VECTOR
// generated by the original encryption.
// Could this have been initialized via the Rfc k1/k2?
decAlg.IV = encAlg.IV;
MemoryStream decryptionStreamBacking = new MemoryStream();
CryptoStream decrypt = new CryptoStream(
decryptionStreamBacking, decAlg.CreateDecryptor(), CryptoStreamMode.Write);
decrypt.Write(edata1, 0, edata1.Length);
decrypt.Flush();
decrypt.Close();
// Erase memory for optimum secrecy?
k2.Reset();
string data2 = new UTF8Encoding(false).GetString(decryptionStreamBacking.ToArray());
if (!data1.Equals(data2))
{
Console.WriteLine("Error: The two values are not equal.");
}
else
{
Console.WriteLine("The two values are equal.");
Console.WriteLine("k1 iterations: {0}", k1.IterationCount);
Console.WriteLine("k2 iterations: {0}", k2.IterationCount);
}
//// }
//// catch (Exception e)
//// {
//// Console.WriteLine("Error: {0}", e);
//// }
Assert.AreEqual(data2, data1);
Assert.IsTrue(data1.Equals(data2));
}
示例5: Decrypt
/// <summary>
/// Decripts a byte array with a given password and salt.
/// </summary>
/// <param name="encryptedData">A byte array containing the data you would like decrypted.</param>
/// <param name="password">The password used to derive the key.</param>
/// <param name="salt">The key salt used to derive the key.</param>
/// <returns>A string containing the decrypted data.</returns>
public static string Decrypt(byte[] encryptedData, string password, string salt)
{
AesManaged aes = null;
MemoryStream memoryStream = null;
CryptoStream cryptoStream = null;
string decryptedText = "";
try
{
//Generate a Key based on a Password, Salt and HMACSHA1 pseudo-random number generator
var rfc2898 = new Rfc2898DeriveBytes(password, Encoding.Unicode.GetBytes(salt));
//Create AES algorithm with 256 bit key and 128-bit block size
aes = new AesManaged();
aes.Key = rfc2898.GetBytes(aes.KeySize / 8);
rfc2898.Reset(); //neede to be WinRT compatible
aes.IV = rfc2898.GetBytes(aes.BlockSize / 8);
//Create Memory and Crypto Streams
memoryStream = new MemoryStream();
cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Write);
//Decrypt Data
cryptoStream.Write(encryptedData, 0, encryptedData.Length);
cryptoStream.FlushFinalBlock();
//Return Decrypted String
var decryptBytes = memoryStream.ToArray();
decryptedText = Encoding.Unicode.GetString(decryptBytes, 0, decryptBytes.Length);
}
catch (Exception eDecrypt)
{
}
finally
{
if (cryptoStream != null)
cryptoStream.Close();
if (memoryStream != null)
memoryStream.Close();
if (aes != null)
aes.Clear();
}
return decryptedText;
}
示例6: Encrypt
/// <summary>
/// Encrypts a string with a given password and salt.
/// </summary>
/// <param name="plainText">The information to encrypt.</param>
/// <param name="password">The password used to derive the key.</param>
/// <param name="salt">The key salt used to derive the key.</param>
/// <exception cref="System.ArgumentException">
/// The specified salt size is smaller than 8 bytes or the iteration count is less than 1.
/// </exception>
/// <returns>A byte array with the encrypted version of the plainText.</returns>
public static byte[] Encrypt(string plainText, string password, string salt)
{
AesManaged aes = null;
MemoryStream memoryStream = null;
CryptoStream cryptoStream = null;
try
{
//Generate a Key based on a Password, Salt and HMACSHA1 pseudo-random number generator
var rfc2898 = new Rfc2898DeriveBytes(password, Encoding.Unicode.GetBytes(salt));
//Create AES algorithm with 256 bit key and 128-bit block size
aes = new AesManaged();
aes.Key = rfc2898.GetBytes(aes.KeySize / 8);
rfc2898.Reset(); //needed for WinRT compatibility
aes.IV = rfc2898.GetBytes(aes.BlockSize / 8);
//Create Memory and Crypto Streams
memoryStream = new MemoryStream();
cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write);
//Encrypt Data
var data = Encoding.Unicode.GetBytes(plainText);
cryptoStream.Write(data, 0, data.Length);
cryptoStream.FlushFinalBlock();
//Return encrypted data
return memoryStream.ToArray();
}
catch (Exception eEncrypt)
{
return null;
}
finally
{
if (cryptoStream != null)
cryptoStream.Close();
if (memoryStream != null)
memoryStream.Close();
if (aes != null)
aes.Clear();
}
}