CryptoStream:要解密的数据长度无效。相同的代码不会在相同的数据上生成错误
好吧,这对我来说很奇怪。我有这段代码,它可以工作:
using (MemoryStream memStream = new MemoryStream(inBytes))
using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
using (CryptoStream cs = new CryptoStream(memStream, decryptor, CryptoStreamMode.Read))
{
byte[] buffer;
if (inBytes.Length < (1024 * 10)) buffer = new byte[inBytes.Length];
else buffer = new byte[(1024 * 10)];
long readBytes = 0;
long totalBytes = inStream.Length;
int currBytes;
while (readBytes < totalBytes)
{
currBytes = cs.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, currBytes);
readBytes += currBytes;
}
}
这会将解密的数据写入文件。
然后我有这段代码,它执行完全相同的操作,除了它写入(并返回)MemoryStream :
using(MemoryStream memStream = new MemoryStream(inBytes))
using(MemoryStream ms = new MemoryStream())
using (CryptoStream cs = new CryptoStream(memStream, decryptor, CryptoStreamMode.Read))
{
byte[] buffer;
if (inBytes.Length < (1024 * 10)) buffer = new byte[inBytes.Length];
else buffer = new byte[(1024 * 10)];
long readBytes = 0;
long totalBytes = inStream.Length;
int currBytes;
while (readBytes < totalBytes)
{
currBytes = cs.Read(buffer, 0, buffer.Length);
ms.Write(buffer, 0, currBytes);
readBytes += currBytes;
}
return ms;
}
在行 currBytes = cs.Read(buffer, 0, buffer.Length)
我收到错误“要解密的数据长度无效”,但仅限于第二组,而不是第一组。 ICryptoTransform
“解密器”是通过通用方法创建的,而且我知道它使用相同的密钥。
谁能告诉我为什么我在第一个实例中不会出现此错误,但在第二个实例中会出现此错误,以及(更重要的是)如何修复它。
是的,我知道 DES 并不是最好的加密方法。这是概念验证的本质,在生产环境中永远不会出现。
Okay, this weird, to me. I have this code, which works:
using (MemoryStream memStream = new MemoryStream(inBytes))
using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
using (CryptoStream cs = new CryptoStream(memStream, decryptor, CryptoStreamMode.Read))
{
byte[] buffer;
if (inBytes.Length < (1024 * 10)) buffer = new byte[inBytes.Length];
else buffer = new byte[(1024 * 10)];
long readBytes = 0;
long totalBytes = inStream.Length;
int currBytes;
while (readBytes < totalBytes)
{
currBytes = cs.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, currBytes);
readBytes += currBytes;
}
}
This writes decrypted data out to a file.
Then I have this code which does exactly the same thing, except it writes to (and returns) a MemoryStream
:
using(MemoryStream memStream = new MemoryStream(inBytes))
using(MemoryStream ms = new MemoryStream())
using (CryptoStream cs = new CryptoStream(memStream, decryptor, CryptoStreamMode.Read))
{
byte[] buffer;
if (inBytes.Length < (1024 * 10)) buffer = new byte[inBytes.Length];
else buffer = new byte[(1024 * 10)];
long readBytes = 0;
long totalBytes = inStream.Length;
int currBytes;
while (readBytes < totalBytes)
{
currBytes = cs.Read(buffer, 0, buffer.Length);
ms.Write(buffer, 0, currBytes);
readBytes += currBytes;
}
return ms;
}
On the line currBytes = cs.Read(buffer, 0, buffer.Length)
I receive the error "Length of the data to decrypt is invalid" but only on the second set, not the first. The ICryptoTransform
"decryptor" is created from a common method, and I know it's using the same key.
Can anyone tell me why I would not be getting this error in the first instance, but would be in the second, and (more importantly) how to fix it.
And, yes, I know DES is not the best encryption method ever. This is something in the nature of a proof-of-concept that will never see the light of day in a production environment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我今天遇到了这个错误,结果发现我在一个函数中使用 ASCII 将一个源字符串转换为字节数组,而在另一个函数中使用 Base64 将另一个源字符串转换为字节数组。
尽管您的输入长度正确,但它们可能没有使用相同的编码。
I ran into this error today and it turned out that I had converted one source string into a byte array using ASCII in one function and the other using Base64 in a different function.
It may be that although your inputs are of the correct length they are not using the same encoding.
尝试将这些检查添加到这两段代码中。我强烈怀疑其中一个或两个都失败了:
Try to add these checks to both pieces of code. I strongly suspect one or both of these fails: