local procedure AESEncryptionExample() var RijndaelCryptography: Codeunit "Rijndael Cryptography"; Base64Convert: Codeunit "Base64 Convert"; EncryptedText: Text; DecryptedText: Text; begin RijndaelCryptography.SetEncryptionData(Base64Convert.ToBase64('1106198321061984'), Base64Convert.ToBase64('ITGateWayXyZ1234')); RijndaelCryptography.SetBlockSize(128); RijndaelCryptography.SetCipherMode('CBC'); RijndaelCryptography.SetPaddingMode('PKCS7'); EncryptedText := RijndaelCryptography.Encrypt('Hello world'); DecryptedText := RijndaelCryptography.Decrypt(EncryptedText); Message('EncryptedText: %1, DecryptedText: %2', EncryptedText, DecryptedText); end;
Answers
Yes, you can
AES is just a variant of Rijndael algorithm.
https://en.wikipedia.org/wiki/Advanced_Encryption_Standard
AES always takes blocks of 128 bits size and accepts three variants of the encryption key: 128, 192 and 256 bits and it's implemented in Business Central.
I had to change your initialization vector (IV) a little bit, because it must contain exactly 16 bytes.
Also I have used a PKCS7 padding, because if you don't use any padding at all, then you must be sure that your input (text that you want to encrypt) is divisible to complete blocks (which are 128 bits long).
Here is sample of code that does that:
by the way for padding PKCS7, is it consider the same like NoPadding?
The thing is that when you encrypt something using AES, it is divided into blocks of text, 128 bits each. If your input is not a mulitply of 128 bits, then it cannot be divided into complete (128bits sized) blocks and that causes errors.
That is where padding comes to help. When you use padding then your input is derived so it can be divided into complete blocks, so there is no issue.
You just have to use the same padding while decrypting, so you get proper results
what should i put as nopadding?
'None' should work aswell, why does not it work for you? Do you get an error or what?
you are right, once i remove the padding it works.
thank you so much Dreez !!
The error you've got above is saying that your input cannot be divided into complete blocks.
That's what I was trying to explain to you earlier... If you don't choose any padding you have to make sure, that your input is a multiple of 128 bits.
The 'None' padding is correct for what you want. And the message above is correct. Your input is just incorrect.
and it works
thanks a lot