gpt4 book ai didi

c# - 文件解密从结尾处丢失 ~10 个字符

转载 作者:行者123 更新时间:2023-11-30 20:13:21 25 4
gpt4 key购买 nike

我已经使用 RC2CryptoServiceProvider 编写了加密/解密方法在 C# 中,出于某种原因,我无法让我的解密器解密最后几个字节。该文件似乎刚刚被切断。我的加密方法如下:

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

虽然我的解密看起来像:

    public static byte[] DecryptString(byte[] input, string password, int originalSize)
{
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 decryptor = RC2.CreateDecryptor(pbeKey, IV);

MemoryStream msDecrypt = new MemoryStream();
CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Write);

csDecrypt.Write(input, 0, originalSize);
// csDecrypt.FlushFinalBlock();

char[] decrypted = new char[input.Length];
decrypted = System.Text.Encoding.UTF8.GetChars(msDecrypt.ToArray());
return msDecrypt.ToArray();

}

char[] decrypted正在返回解密的整个文件,除了文件以 </LudoData> 结尾解密时,我只得到第一个 <特点。

我一直在研究事物的长度,但没有任何改变。在我的具体情况下,input长度为 11296,originalSize大小为 11290。但是,decrypted解密时最终大小为 11280。什么给!

最佳答案

是否有理由将 Flush() 注释掉?您是否尝试过完全关闭流?

关于c# - 文件解密从结尾处丢失 ~10 个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1632351/

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