.NET AES 填充模式更改加密数据大小

发布于 2025-01-18 12:30:11 字数 917 浏览 2 评论 0原文

我不一定有问题,我更好奇。如果我将 AES 填充模式设置为 PKCS7,并加密长度为 128 的 byte[],则输出的 byte[] 长度为 144。

var input = new string('0', 128);
var inputBytes = Encoding.UTF8.GetBytes(input);

using var aes = Aes.Create();

Console.WriteLine(aes.BlockSize); // 128 (bits)

aes.Padding = PaddingMode.PKCS7;

using var encryptedMessageStream = new MemoryStream();
using (var cryptoStream = new CryptoStream(encryptedMessageStream, aes.CreateEncryptor(), CryptoStreamMode.Write, leaveOpen: true))
{
    cryptoStream.Write(inputBytes);
}

var encryptedBytes = encryptedMessageStream.ToArray(); // 144 length

如果我不做任何更改,期望将填充模式切换为 None,那么加密字节的长度将为 128(与我的输入长度相同)。因此,即使我的输入数据没有改变,并且输入长度可以被 AES 块大小(8 字节)整除,填充模式也会改变最终输出。据我的理解,它不应该,因为我的输入长度可以被 AES 块大小整除。那么为什么 PKCS7None 会给出不同的输出大小呢?

当通过解密运行两种模式时,我很好地得到了原始消息。

I don't necessarily have a problem, I am more curious. If I set the AES padding mode to PKCS7, and encrypt a byte[] of length 128, the output byte[] is length 144.

var input = new string('0', 128);
var inputBytes = Encoding.UTF8.GetBytes(input);

using var aes = Aes.Create();

Console.WriteLine(aes.BlockSize); // 128 (bits)

aes.Padding = PaddingMode.PKCS7;

using var encryptedMessageStream = new MemoryStream();
using (var cryptoStream = new CryptoStream(encryptedMessageStream, aes.CreateEncryptor(), CryptoStreamMode.Write, leaveOpen: true))
{
    cryptoStream.Write(inputBytes);
}

var encryptedBytes = encryptedMessageStream.ToArray(); // 144 length

If I change nothing expect switch the padding mode to None, then the encrypted bytes will have length 128 (same length as my input). So even though my input data has not changed, and the input length is evenly divisible by the AES block size (8 bytes), the padding mode is changing the final output. As far as my understanding goes, it shouldn't, because, my input length is evenly divisible by the AES block size. So why would PKCS7, and None be giving different output sizes?

When running both modes through decryption, I get the original message back just fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

尸血腥色 2025-01-25 12:30:11

PKCS 填充要求至少添加一个字节的填充:

填充字节总数至少为1,并且是
这是为了使数据长度达到整数倍所必需的
密码算法块大小。

因此,虽然您可能认为圆块大小的明文需要零字节,但这不是有效的填充量,因为最小数量是 1 字节。 (对此的解释是,否则解密器无法区分恰好以 0x01 结尾的圆块大小的明文和用单个 填充的短一字节的明文0x01。)

PKCS padding requires that at least one byte of padding be added:

The total number of padding bytes is at least one, and is the number
that is required in order to bring the data length up to a multiple of
the cipher algorithm block size.

So, while you may feel that your round-block-size plaintext requires zero bytes, that's not a valid amount of padding because the minimum number is 1 byte. (The explanation of this is that otherwise it's impossible for the decipherer to differentiate between a round-block-size plaintext that happens to end in 0x01 and a one byte shorter plaintext that's padded with a single 0x01.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文