本文整理汇总了C#中DESCryptoServiceProvider.GenerateIV方法的典型用法代码示例。如果您正苦于以下问题:C# DESCryptoServiceProvider.GenerateIV方法的具体用法?C# DESCryptoServiceProvider.GenerateIV怎么用?C# DESCryptoServiceProvider.GenerateIV使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DESCryptoServiceProvider
的用法示例。
在下文中一共展示了DESCryptoServiceProvider.GenerateIV方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Encrypt
public string Encrypt(string item, string password)
{
if (item == null) {
throw new ArgumentNullException ("item");
}
if (string.IsNullOrEmpty (password)) {
throw new ArgumentNullException ("password");
}
var des = new DESCryptoServiceProvider ();
des.GenerateIV ();
var rfc2898DeriveBytes = new Rfc2898DeriveBytes (password, des.IV, ITERATIONS);
byte[] key = rfc2898DeriveBytes.GetBytes (8);
using (var memoryStream = new MemoryStream()) {
using (var cryptoStream = new CryptoStream(memoryStream, des.CreateEncryptor(key, des.IV), CryptoStreamMode.Write)) {
memoryStream.Write (des.IV, 0, des.IV.Length);
byte[] bytes = Encoding.UTF8.GetBytes (item);
cryptoStream.Write (bytes, 0, bytes.Length);
cryptoStream.FlushFinalBlock ();
return Convert.ToBase64String (memoryStream.ToArray ());
}
}
}
示例2: 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;
}
示例3: Encrypt
public string Encrypt(string plainText, string password)
{
if (plainText == null)
{
throw new ArgumentNullException("plainText");
}
if (string.IsNullOrEmpty(password))
{
throw new ArgumentNullException("password");
}
// create instance of the DES crypto provider
var des = new DESCryptoServiceProvider();
// generate a random IV will be used a salt value for generating key
des.GenerateIV();
// use derive bytes to generate a key from the password and IV
var rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, des.IV, Iterations);
// generate a key from the password provided
byte[] key = rfc2898DeriveBytes.GetBytes(8);
// encrypt the plainText
using (var memoryStream = new MemoryStream())
using (var cryptoStream = new CryptoStream(memoryStream, des.CreateEncryptor(key, des.IV), CryptoStreamMode.Write))
{
// write the salt first not encrypted
memoryStream.Write(des.IV, 0, des.IV.Length);
// convert the plain text string into a byte array
byte[] bytes = Encoding.UTF8.GetBytes(plainText);
// write the bytes into the crypto stream so that they are encrypted bytes
cryptoStream.Write(bytes, 0, bytes.Length);
cryptoStream.FlushFinalBlock();
return Convert.ToBase64String(memoryStream.ToArray());
}
}
示例4: 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;
}