本文整理汇总了C#中PasswordDeriveBytes.CryptDeriveKey方法的典型用法代码示例。如果您正苦于以下问题:C# PasswordDeriveBytes.CryptDeriveKey方法的具体用法?C# PasswordDeriveBytes.CryptDeriveKey怎么用?C# PasswordDeriveBytes.CryptDeriveKey使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PasswordDeriveBytes
的用法示例。
在下文中一共展示了PasswordDeriveBytes.CryptDeriveKey方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
public static void Main() {
string PlainText = "Titan";
byte[] PlainBytes = new byte[5];
PlainBytes = Encoding.ASCII.GetBytes(PlainText.ToCharArray());
PrintByteArray(PlainBytes);
byte[] CipherBytes = new byte[8];
PasswordDeriveBytes pdb = new PasswordDeriveBytes("Titan", null);
byte[] IV = new byte[8];
byte[] Key = pdb.CryptDeriveKey("RC2", "SHA1", 40, IV);
PrintByteArray(Key);
PrintByteArray(IV);
// Now use the data to encrypt something
RC2CryptoServiceProvider rc2 = new RC2CryptoServiceProvider();
Console.WriteLine(rc2.Padding);
Console.WriteLine(rc2.Mode);
ICryptoTransform sse = rc2.CreateEncryptor(Key, IV);
MemoryStream ms = new MemoryStream();
CryptoStream cs1 = new CryptoStream(ms, sse, CryptoStreamMode.Write);
cs1.Write(PlainBytes, 0, PlainBytes.Length);
cs1.FlushFinalBlock();
CipherBytes = ms.ToArray();
cs1.Close();
Console.WriteLine(Encoding.ASCII.GetString(CipherBytes));
PrintByteArray(CipherBytes);
ICryptoTransform ssd = rc2.CreateDecryptor(Key, IV);
CryptoStream cs2 = new CryptoStream(new MemoryStream(CipherBytes), ssd, CryptoStreamMode.Read);
byte[] InitialText = new byte[5];
cs2.Read(InitialText, 0, 5);
Console.WriteLine(Encoding.ASCII.GetString(InitialText));
PrintByteArray(InitialText);
}
示例2: TestRepeated
public static Boolean TestRepeated()
{
Boolean bRes = true;
int l, key_size;
Char[] ach;
String s;
Byte[] salt, the_key, temp_key, iv = new Byte[8];
PasswordDeriveBytes pdb;
for(int i=0; i<NO_PASSES; i++) {
l = Rnd.Next(MAX_PASS_LEN)+1;
ach = new Char[l];
for(int k=0; k<l; k++) ach[k] = (Char)(Rnd.Next(26)+65);
s = new String(ach);
salt = new Byte[Rnd.Next(MAX_SALT_LEN)];
Rnd.NextBytes(salt);
key_size = Rnd.Next(128);
Rnd.NextBytes(iv);
pdb = new PasswordDeriveBytes(s, salt);
the_key = pdb.CryptDeriveKey("RC2", "SHA1", /*key_size*/ 128, iv);
Console.WriteLine("--------------------------------------");
PrintByteArray(the_key);
for (int j=0; j<MAX_COMP;j++) {
temp_key = pdb.CryptDeriveKey("RC2", "SHA1", /*key_size*/ 128, iv);
Console.WriteLine("--------------------------------------");
PrintByteArray(temp_key);
if (!Compare(the_key, temp_key)) {
bRes = false;
Console.WriteLine("Two passes of CryptDeriveKey yielded different results");
break;
}
}
if (bRes == false) break;
}
return bRes;
}
示例3: GetScopeKey
// Get the encryption key to use to protect memory for a scope.
private static byte[] GetScopeKey(MemoryProtectionScope scope, byte[] salt)
{
String key;
PasswordDeriveBytes derive;
if(scope == MemoryProtectionScope.SameLogon)
{
key = Environment.UserName;
}
else
{
key = Environment.UserName + "/" + Environment.MachineName;
}
if(salt == null)
{
salt = new byte [16];
}
derive = new PasswordDeriveBytes(key, salt);
return derive.CryptDeriveKey("Rijndael", "SHA1", 16, null);
}
示例4: Encrypt
private static string Encrypt(string strText)
{
string key = "&%#@?,:*";
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.IV = new byte[8];
PasswordDeriveBytes pdb = new PasswordDeriveBytes(key, new byte[-1 + 1]);
des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, new byte[8]);
MemoryStream ms = new MemoryStream((strText.Length * 2) - 1);
CryptoStream encStream = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
byte[] plainBytes = Encoding.UTF8.GetBytes(strText);
encStream.Write(plainBytes, 0, plainBytes.Length);
encStream.FlushFinalBlock();
byte[] encryptedBytes = new byte[(int)ms.Length - 1 + 1];
ms.Position = 0;
ms.Read(encryptedBytes, 0, (int)ms.Length);
encStream.Close();
return Convert.ToBase64String(encryptedBytes);
}
示例5: TestKnownValue_CryptDeriveKey
private static byte[] TestKnownValue_CryptDeriveKey(HashAlgorithmName hashName, string password, string alg, int keySize, byte[] salt, byte[] expected)
{
byte[] output;
byte[] iv = new byte[8];
using (var deriveBytes = new PasswordDeriveBytes(password, salt))
{
output = deriveBytes.CryptDeriveKey(alg, hashName.Name, keySize, iv);
}
Assert.Equal(expected, output);
// For these tests, the returned IV is always zero
Assert.Equal(new byte[8], iv);
return output;
}
示例6: CryptDeriveKey_Invalid_IV
public static void CryptDeriveKey_Invalid_IV()
{
using (var deriveBytes = new PasswordDeriveBytes(TestPassword, s_testSalt))
{
Assert.Throws<CryptographicException>(() => deriveBytes.CryptDeriveKey("RC2", "SHA1", 128, null));
Assert.Throws<CryptographicException>(() => deriveBytes.CryptDeriveKey("RC2", "SHA1", 128, new byte[1]));
}
}
示例7: CryptDeriveKey_Invalid_HashAlgorithm
public static void CryptDeriveKey_Invalid_HashAlgorithm()
{
using (var deriveBytes = new PasswordDeriveBytes(TestPassword, s_testSalt))
{
Assert.Throws<CryptographicException>(() => deriveBytes.CryptDeriveKey("RC2", "BADALG", 128, s_testSalt));
}
}
示例8: CryptDeriveKey_Invalid_KeyLength
public static void CryptDeriveKey_Invalid_KeyLength()
{
using (var deriveBytes = new PasswordDeriveBytes(TestPassword, s_testSalt))
{
Assert.ThrowsAny<CryptographicException>(() => deriveBytes.CryptDeriveKey("RC2", "SHA1", 127, s_testSalt));
Assert.ThrowsAny<CryptographicException>(() => deriveBytes.CryptDeriveKey("RC2", "SHA1", 129, s_testSalt));
}
}
示例9: TestKnown
public static Boolean TestKnown()
{
Boolean bRes = true;
Byte[] IV = new Byte[8];
Byte[] PlainText = {0,1,2,3,4,5,6,7};
Byte[] KnownVector = {0x7A, 0x50, 0x39, 0x82, 0xB5, 0x0E, 0xB0, 0x0D, 0x1F, 0x37, 0x9D, 0xC8, 0x36, 0x09, 0xD3, 0xFF};
PasswordDeriveBytes pdb = new PasswordDeriveBytes("simplepassword", null);
Byte[] the_key = pdb.CryptDeriveKey("RC2", "MD5", 40, IV);
RC2CryptoServiceProvider rc2 = new RC2CryptoServiceProvider();
ICryptoTransform sse = rc2.CreateEncryptor(the_key, IV);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, sse, CryptoStreamMode.Write);
cs.Write(PlainText,0,PlainText.Length);
cs.FlushFinalBlock();
byte[] ciphertext = ms.ToArray();
cs.Close();
Console.WriteLine("--- Cipher Text : ----");
PrintByteArray(ciphertext);
Console.WriteLine("--- Known vector : ----");
PrintByteArray(KnownVector);
if(!Compare(ciphertext, KnownVector)) {
Console.WriteLine("Known and calculated values differ!");
bRes = false;
}
return bRes;
}
示例10: DeriveKeyNullIv
/// <summary>
/// Derive key, null IV
/// </summary>
private static void DeriveKeyNullIv()
{
string password = "pwd";
byte[] salt = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };
PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, salt);
pdb.CryptDeriveKey("RC2", "MD5", 21, null);
return;
}