Jurriaan Buitenweg
Jurriaan Buitenweg

Reputation: 422

How can i decrypt byte[] of more than 16 bytes from AES

So I have a byte[] with a length of 1622 bytes but I want to decrypt it all but AES has only a blocksize of 128 bits. If im trying to split it up to blocks of 16 bytes i get this exception: System.Security.Cryptography.CryptographicException Additional information: Input buffer contains insufficient data. Can it be that rawDataArea % 16 != 0?

Encryptor:

        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);

Decryptor

        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);
        }

Upvotes: 0

Views: 751

Answers (1)

usr
usr

Reputation: 171218

You're executing i += 16 two times per loop iteration. You don't need to transform in 16 byte chunks anyway. Use CryptoStream and write any amount you like such as 4KB. Most tutorials do this. Maybe you found a bad tutorial.

Upvotes: 3

Related Questions