CryptoStream:要解密的数据长度无效。相同的代码不会在相同的数据上生成错误

发布于 2025-01-06 11:47:48 字数 1678 浏览 1 评论 0原文

好吧,这对我来说很奇怪。我有这段代码,它可以工作:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

俯瞰星空 2025-01-13 11:47:48

我今天遇到了这个错误,结果发现我在一个函数中使用 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.

你爱我像她 2025-01-13 11:47:48

尝试将这些检查添加到这两段代码中。我强烈怀疑其中一个或两个都失败了:

if ( inStream.Length != inBytes.Length )
  throw new Exception("inBytes read incorrectly");
if ( inBytes.Length % 8 == 0 )
  throw new Exception("inBytes is not a valid DES encryption");

Try to add these checks to both pieces of code. I strongly suspect one or both of these fails:

if ( inStream.Length != inBytes.Length )
  throw new Exception("inBytes read incorrectly");
if ( inBytes.Length % 8 == 0 )
  throw new Exception("inBytes is not a valid DES encryption");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文