【发布时间】:2009-10-26 16:03:51
【问题描述】:
我正在尝试使用以下代码加密一些数据:
public static byte[] EncryptString(byte[] input, string password)
{
PasswordDeriveBytes pderiver = new PasswordDeriveBytes(password, null);
byte[] ivZeros = new byte[8];
byte[] pbeKey = pderiver.CryptDeriveKey("RC2", "MD5", 128, ivZeros);
RC2CryptoServiceProvider RC2 = new RC2CryptoServiceProvider();
byte[] IV = new byte[8];
ICryptoTransform encryptor = RC2.CreateEncryptor(pbeKey, IV);
MemoryStream msEncrypt = new MemoryStream();
CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
csEncrypt.Write(input, 0, input.Length);
csEncrypt.FlushFinalBlock();
return msEncrypt.ToArray();
}
但是,当它初始化我的 CryptoStream 对象时,它会引发以下错误:
“流不支持搜索。” 为了澄清,上面的代码中没有错误处理,所以只是运行它不会“破坏”,persay。但单步执行代码时,CryptoStream 对象在初始化后将在其属性中显示此错误。
这是为什么?我该如何避免呢?
【问题讨论】:
-
因为这个流不支持搜索。为什么要避免它?只要你不使用 Seek,一切都应该没问题。
标签: c# encryption cryptography