本文整理汇总了C#中DESCryptoServiceProvider.GenerateKey方法的典型用法代码示例。如果您正苦于以下问题:C# DESCryptoServiceProvider.GenerateKey方法的具体用法?C# DESCryptoServiceProvider.GenerateKey怎么用?C# DESCryptoServiceProvider.GenerateKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DESCryptoServiceProvider
的用法示例。
在下文中一共展示了DESCryptoServiceProvider.GenerateKey方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static int Main ()
{
string filename = Path.Combine (AppDomain.CurrentDomain.BaseDirectory,
"encrypt.tmp");
string data = "this is sensitive data";
DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
des.GenerateIV ();
des.GenerateKey ();
// ----------- WRITING ENCRYPTED SERIALIZED DATA ------------------
Stream stream = new FileStream (filename, FileMode.Create, FileAccess.Write);
stream = new CryptoStream (stream, des.CreateEncryptor (), CryptoStreamMode.Write);
BinaryFormatter bformatter = new BinaryFormatter ();
bformatter.Serialize (stream, data);
stream.Close ();
stream = null;
bformatter = null;
data = string.Empty;
// ----------- READING ENCRYPTED SERIALIZED DATA ------------------
stream = new FileStream (filename, FileMode.Open, FileAccess.Read);
stream = new CryptoStream (stream, des.CreateDecryptor (), CryptoStreamMode.Read);
bformatter = new BinaryFormatter ();
data = (string) bformatter.Deserialize (stream);
stream.Close ();
//----------- CHECK RESULTS ----------------
if (data != "this is sensitive data")
return 1;
return 0;
}
示例2: Test
static Boolean Test()
{
Byte[] PlainText = {0, 1, 2, 3, 4, 5, 6, 7}; //, 8, 9, 10, 11, 12, 13, 14, 15};
Byte[] Key = {1, 1, 1, 1, 1, 1, 1, 1};
Byte[] IV = {1, 1, 1, 1, 1, 1, 1, 1};
Console.WriteLine("Encrypting the following bytes:");
PrintByteArray(PlainText);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Mode = CipherMode.ECB;
// des.FeedbackSize = 0;
des.Padding = PaddingMode.PKCS7;
int depth = 10;
Stream[] encStream_arr = new Stream[depth];
Stream[] decStream_arr = new Stream[depth];
ICryptoTransform[] decTrans_array = new ICryptoTransform[depth];
encStream_arr[0] = new MemoryStream();
decStream_arr[0] = new MemoryStream();
for (int i=1; i<depth; i++) {
des.GenerateKey();
des.GenerateIV();
encStream_arr[i] = new CryptoStream(encStream_arr[i-1],des.CreateEncryptor(),CryptoStreamMode.Write);
decTrans_array[i] = des.CreateDecryptor();
}
for (int j=1; j<depth; j++) {
decStream_arr[j] = new CryptoStream(decStream_arr[j-1],decTrans_array[depth-j],CryptoStreamMode.Write);
}
Console.WriteLine("DES default key size = " + des.KeySize);
Console.WriteLine("SSE mode = " + des.Mode);
encStream_arr[depth-1].Write(PlainText,0,PlainText.Length);
((CryptoStream)encStream_arr[depth-1]).FlushFinalBlock();
byte[] CipherText = ((MemoryStream)encStream_arr[0]).ToArray();
encStream_arr[depth-1].Close();
Console.WriteLine("Cyphertext:");
PrintByteArray(CipherText);
Console.WriteLine("Decrypting...");
Console.WriteLine("SSD mode = " + des.Mode);
decStream_arr[depth-1].Write(CipherText,0,CipherText.Length);
((CryptoStream)decStream_arr[depth-1]).FlushFinalBlock();
byte[] NewPlainText = ((MemoryStream)decStream_arr[0]).ToArray();
decStream_arr[depth-1].Close();
PrintByteArray(NewPlainText);
if (!Compare(PlainText, NewPlainText)) {
Console.WriteLine("ERROR: roundtrip failed");
return false;
}
return true;
}