Bad AES decoding in certain contexts

In your documentation and example you suggest to code and decode messages with format raw and object Coder, but this method do no work in real messages sent in http/Rest mode between server and client.

I found a turnaround with TIdEncoder / TIdDecoder and input/output format of cyphered/decyphered in row.

My develop platform is Delphi 13 64 bit.

This seems works correctly:

for AES encoding

aes:=TAESEncryption.Create();
aes.AType:=atCBC;
aes.inputFormat:=raw;
aes.keyLength:=kl128;
aes.key:=Key;
aes.outputFormat:=raw; (and not hexa)
aes.paddingMode:=TPaddingMode.PKCS7;
aes.Unicode:=yesUni;
aes.IVMode:=TIVMode.rand;
cyp:=aes.Encrypt(msg);
Encoder := TIdEncoderMIME.Create(nil);
cyp:=TIdEncoderMIME.EncodeString(cyp, IndyTextEncoding_UTF8);

and for AES decoding

Decoder := TIdDecoderMIME.Create(nil);
cyp:=TIdDecoderMIME.DecodeString(cyp, IndyTextEncoding_UTF8);
aes:=TAESEncryption.Create();
aes.AType:=atCBC;
aes.inputFormat:=raw;
aes.keyLength:=kl128;
aes.Unicode:=yesUni;
aes.key:=Key;
aes.outputFormat:=raw;
aes.paddingMode:=TPaddingMode.PKCS7;
aes.IVMode:=rand;
msg:=aes.Decrypt(cyp);

(and not Conv := TConvert.Create(hexa); // because the previous output was in hex cipher := Conv.FormatToChar(cip)

Crypto Pack documentation is not enough accuracy as VCL Pack documentation; this is for me a real cost because hours lost to test cases, platforms, etc. . .

Hi,

Your example is correct and it is true that the documentation doesn’t cover all cases and modes and is sometimes outdated.

It is better to refer to the examples for code excerpts as they are normally compiled with the latest library version.

Regards,

bernard