如何从AES解密超过16个字节的byte []

时间:2016-09-20 12:04:35

标签: c# encryption cryptography aes

所以我有一个长度为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);
        }

1 个答案:

答案 0 :(得分:3)

每个循环迭代执行i += 16两次。无论如何,您不需要转换16个字节的块。使用CryptoStream并写下您喜欢的任何金额,例如4KB。大多数教程都是这样做的。也许你发现了一个糟糕的教程。

相关问题