使用RC2CryptoServiceProvider 进行加密,在Framework1.1 下,iv可以使用的少于8位.但是在 Framework 2.0下IV至少要8位,否则会抛出异常!反射了Framework类库,就单单的加了一条,长度小于8位就抛异常.MS 一直是向后兼容,s可是现在不是使Framework1.1 下IV少于7位的加密数据不能在Framework 2.0下使用了吗?
下面的代码在Framework 1.1下可以通过,但Framework 2.0下不能通过,请注意IV长度。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace RC2CryptoServiceProvider_Examples
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
class MyMainClass1
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public static void Main()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Create a new instance of the RC2CryptoServiceProvider class
// and automatically generate a Key and IV.
RC2CryptoServiceProvider rc2CSP = new RC2CryptoServiceProvider();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine("Effective key size is {0} bits.", rc2CSP.EffectiveKeySize);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
string strIV = "1234567";
// Get the key and IV.
byte[] key = rc2CSP.Key;
byte[] IV ;//= rc2CSP.IV;
IV = ASCIIEncoding.ASCII.GetBytes(strIV);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Get an encryptor.
ICryptoTransform encryptor = rc2CSP.CreateEncryptor(key, IV);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Encrypt the data as an array of encrypted bytes in memory.
MemoryStream msEncrypt = new MemoryStream();
CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Convert the data to a byte array.
string original = "Here is some data to encrypt.";
byte[] toEncrypt = Encoding.ASCII.GetBytes(original);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Write all data to the crypto stream and flush it.
csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
csEncrypt.FlushFinalBlock();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Get the encrypted array of bytes.
byte[] encrypted = msEncrypt.ToArray();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**/
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////////////////////////////////////////////////////////
// This is where the data could be transmitted or saved.
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**/
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////////////////////////////////////////////////////////
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Get a decryptor that uses the same key and IV as the encryptor.
ICryptoTransform decryptor = rc2CSP.CreateDecryptor(key, IV);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Now decrypt the previously encrypted message using the decryptor
// obtained in the above step.
MemoryStream msDecrypt = new MemoryStream(encrypted);
CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
byte[] fromEncrypt = new byte[toEncrypt.Length];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Read the data out of the crypto stream.
csDecrypt.Read(fromEncrypt, 0, toEncrypt.Length);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Convert the byte array back into a string.
String roundtrip = Encoding.ASCII.GetString(fromEncrypt);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Display the original data and the decrypted data.
Console.WriteLine("Original: {0}", original);
Console.WriteLine("Round Trip: {0}", roundtrip);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.ReadLine();
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)