I am attempting to implement AES in CBC mode for both
encryption and decryption using Crypto++ 5.1.

The encryption works fine, however, the decryption throws
the "PKCS #7 block padding found" error from
StreamTransformationFilter::LastPut
case PKCS_PADDING:

The decryption function is

byte *AESDecryptByteArray(byte *bKey, byte *bIV, byte
*bCiphertext, int iLength)
{
        byte *bPlaintext;
        
        bPlaintext = (byte *)malloc(iLength);
        ZeroMemory((void *)bPlaintext, iLength);

        CBC_Mode<AES >::Decryption aes(bKey,
AES::DEFAULT_KEYLENGTH, bIV);
    StreamTransformationFilter aes_dec(aes, new
ArraySink(bPlaintext, iLength));
        
    aes_dec.Put((const unsigned char *)bCiphertext,
iLength);
    aes_dec.MessageEnd();

    return bPlaintext;

}

The calling code is

int main(void)
{
        AutoSeededRandomPool rng;
        byte *bKey;
        byte *bIV;
        byte bPlaintext[16];
        byte *bCiphertext;
        int iLength;
        byte *bFinaltext;

        bKey = (byte *)malloc(AES::DEFAULT_KEYLENGTH);
        bIV = (byte *)malloc(AES::BLOCKSIZE);
        bCiphertext = (byte *)malloc(16);
        bFinaltext = (byte *)malloc(16);

        iLength = 16;
        
        for (int i = 0; i < 32; i++)
                bKey[i] = rng.GenerateByte();
        for (i = 0; i < AES::BLOCKSIZE; i++)
                bIV[i] = rng.GenerateByte();

        strcpy((char*)bPlaintext,"0123456789ABCDEF");

        bCiphertext = AESEncryptByteArray(bKey,bIV, bPlaintext,
iLength);

        bFinaltext = AESDecryptByteArray(bKey, bIV, bCiphertext,
iLength);

        cout << bPlaintext << endl;
        cout << bCiphertext << endl;
        cout << bFinaltext << endl;
        return 0;
}

Any ideas as to why this error is occuring would be much
appreciated

Iain

Reply via email to