我有一个加密的文件,我首先解密它,然后尝试使用memorystream和binaryformatter反序列化它,但是当我试图将反序列化文件分配给一个列表时,我捕获了OutOfMemoryException (文件非常小-17 is )。
byte[] encryptedData = File.ReadAllBytes(fileName);
byte[] result = Decrypt(Algo, key, vector, encryptedData) ;
BinaryFormatter ser = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream(result)) {
try {
files = ser.Deserialize(ms) as List<IItem>;
} catch (OutOfMemoryException) {
} catch (SerializationException) {
MessageBox.Show("Incorrect password!");
return;
}
}files = ser.Deserialize(ms) as List<IItem>; -这是导致1691年解密后异常加密文件大小1696的原因-正常大小。这里解密代码
public byte[] Decode(byte[] data)
{
string key = ASCIIEncoding.ASCII.GetString(rc2.Key);
string IV = ASCIIEncoding.ASCII.GetString(rc2.IV);
ICryptoTransform decryptor = rc2.CreateDecryptor(rc2.Key,rc2.IV);
StringBuilder roundtrip = new StringBuilder();
using (MemoryStream msDecrypt = new MemoryStream(data))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
int b = 0;
do
{
b = csDecrypt.ReadByte();
if (b != -1)
{
roundtrip.Append((char) b);
}
} while (b != -1);
}
}
byte[] decrypted = ASCIIEncoding.ASCII.GetBytes(roundtrip.ToString());
return decrypted;
}发布于 2015-05-10 17:02:54
@MineR and @HansPassant是对的问题是在使用字符的同时解密))我已经更改了我的代码
public byte[] Decode(byte[] data)
{
ICryptoTransform decryptor = rc2.CreateDecryptor(rc2.Key,rc2.IV);
byte[] decrypted = new byte[data.Length];
using (MemoryStream msDecrypt = new MemoryStream(data))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
csDecrypt.Read(decrypted, 0, data.Length);
}
}
return decrypted;
}现在起作用了。都是为了寻求答案。
https://stackoverflow.com/questions/30152479
复制相似问题