【发布时间】:2016-09-20 12:04:35
【问题描述】:
所以我有一个长度为 1622 字节的 byte[],但我想全部解密,但 AES 只有 128 位的块大小。 如果我试图将它分成 16 个字节的块,我会得到这个异常: System.Security.Cryptography.CryptographicException 附加信息:输入缓冲区包含的数据不足。可能是 rawDataArea % 16 != 0?
加密器:
aes256Alg = new AesManaged
{
Key = new byte[] {112,90,16,164,90,221,73,154,246,32,13,102,145,7,57,115,37,5,3,102,205,39,202,231,195,148,202,229,53,138,102,242},
Mode = CipherMode.CBC,
KeySize = 256,
BlockSize = 128,
Padding = PaddingMode.PKCS7,
IV = new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};
ICryptoTransform aes256Encryptor = aes256Alg.CreateEncryptor(aes256Alg.Key,aes256Alg.IV);
解密器
AesManaged aes256AlgCBC;
aes256AlgCBC = new AesManaged
{
Key = new byte[] {190,151,28,108,241,101,254,174,16,11,87,84,239,140,239,85,195,25,78,192,105,109,95,128,160,146,123,31,190,188,181,216},
KeySize = 256,
Mode = CipherMode.CBC,
BlockSize = 128,
Padding = PaddingMode.PKCS7,
IV = new byte[] {199,114,91,241,148,90,133,166,13,52,142,187,101,125,81,73}
};
ICryptoTransform aes256CbcDecryptor = aes256AlgCBC.CreateDecryptor(aes256AlgCBC.Key, aes256AlgCBC.IV);
//byte[] rawDataArea = {0x00 .......} // Length of 1622 copied from hexeditor
List<Byte[]> dataAreaByteList = new List<byte[]>();
//Split the rawDataArea up to blocks of 16 bytes and then adding them to a list
//which later can be converted back to a big array
for (int i = 0; i < rawDataArea.Length; i += 16)
{
byte[] transformedBlock = new byte[] { };
aes128CbcDecryptor.TransformBlock(rawDataArea, i, (i += 16),transformedBlock,i);
dataAreaByteList.Add(transformedBlock);
}
【问题讨论】:
-
字节数组的长度必须是 16 倍。如果不是,那么填充就起作用了。
-
如果要使用
TransformBlock风格的界面,需要在最后一块上使用TransformFinalBlock。具体来说,它允许不等于整个块的数据块。
标签: c# encryption cryptography aes