gpt4 book ai didi

c# - 小文件导致 OutofMemoryException

转载 作者:行者123 更新时间:2023-11-30 12:58:22 26 4
gpt4 key购买 nike

我有一个加密文件,我先对其进行解密,然后尝试使用 memorystream 对其进行反序列化。和 binaryformatter但是当我尝试将反序列化文件分配给列表时,我发现了 OutOfMemoryException (文件真的很小 - 17KB)这是代码

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>; - 这是什么导致异常加密文件大小 1696解密后 1691 - 正常大小。这里是解密代码

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;
}

最佳答案

@MineR 和@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;
}

现在可以了。感谢大家的回答。

关于c# - 小文件导致 OutofMemoryException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30152479/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com