简体   繁体   English

C#AES PKCS#7错误的填充字节?

[英]C# AES PKCS #7 wrong padding bytes?

I have set up a simple symmetric AES-en/decryption in C#, but I'm having problems with the padding. 我已经在C#中设置了一个简单的对称AES-en / decryption,但是我在填充方面遇到了问题。 According to MSDN , the padding bytes for PKCS #7 are supposed to be 0x07 , but in my case it's just zero-bytes ( 0x00 ). 根据MSDN ,PKCS#7的填充字节应为0x07 ,但在我的情况下,它仅为零字节( 0x00 )。

How is this possible? 这怎么可能? It almost seems as if this was not correctly implemented in .NET... 几乎好像这没有在.NET中正确实现...

Here is my code: 这是我的代码:

Aes aes = new AesManaged();
aes.Key = new byte[] { /* ...  */ };
aes.IV = new byte[] { /* ... */ };
// Debugging shows:
// aes.Padding = PaddingMode.PKCS7

// the data to encrypt (1 byte only, to demonstrate padding)
byte[] plainData = new byte[1] { 0xFF };
byte[] encData;

// (encrypt)
using (MemoryStream encStream = new MemoryStream())
{
    using (CryptoStream cryptoStream = new CryptoStream(encStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
    {
        cryptoStream.Write(plainData, 0, plainData.Length);
    }
    encData = encStream.ToArray();
}

// (new length is 16 bytes (128 bits), incl. padding)
plainData = new byte[16];

// (decrypt)
using (MemoryStream decrStream = new MemoryStream(encData))
{
    using (CryptoStream cryptoStream = new CryptoStream(decrStream, aes.CreateDecryptor(), CryptoStreamMode.Read))
    {
        cryptoStream.Read(plainData, 0, plainData.Length);
    }
}

// output:
// 16 bytes,
// 1st byte = 0xFF,
// other 15 bytes = 0x00 (instead of 0x07!)

The decryptor is correctly removing the padding that was applied by the encryptor, thus the zero bytes in your output are simply the un-touched bytes in the original plainData array. 解密程序正确地删除了加密程序所应用的填充,因此输出中的零字节就是原始plainData数组中未触及的字节。 The cryptoStream.Read(...) call returns an integer indicating the number of bytes that were read ( 1 in this case), which you should be using to determine how many bytes in the output array are valid data. cryptoStream.Read(...)调用返回一个整数,该整数指示已读取的字节数(在本例中为1 ),您应该使用该整数来确定输出数组中的有效数据为多少字节。

If for whatever reason you are interested in seeing the padding bytes, you can set aes.Padding = PaddingMode.None; 如果出于某种原因您希望查看填充字节,则可以设置aes.Padding = PaddingMode.None; after the encryption is performed, but before you create the decryptor. 执行加密之后 ,但创建解密器之前 You will then find that cryptoStream.Read(...) returns 16 , and plainData has 0xff as its first byte, followed by 15 bytes of 0x0f padding (not sure why your question indicates you were expecting 0x07 though). 然后,您会发现cryptoStream.Read(...)返回16 ,而plainData的第一个字节为0xff ,然后是15个字节的0x0f填充(不确定您的问题为何表示您期望使用0x07 )。

For PKCS7 mode it should be blocksize - contentsize , ie 16 - 1 = 15 in your case. 对于PKCS7模式,应为blocksize - contentsize ,即您的情况为16-1 = 15。 Your mistake is that you expect it after decryption but padding happens internally before encryption. 您的错误是您希望在解密后执行此操作,但是在加密之前在内部进行填充。 There are no guarantees that plainData will contain padded bytes according to mode choosen. 无法保证plainData将根据选择的模式包含填充字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM