AES Encryption for Cloud BC

julkifli33
Member Posts: 1,092
Hi All,
I need to encrypt for AES Encryption in Cloud BC.
is BC able to do that?
usually i encrypt from here --> https://www.devglan.com/online-tools/aes-encryption-decryption

I need to encrypt for AES Encryption in Cloud BC.
is BC able to do that?
usually i encrypt from here --> https://www.devglan.com/online-tools/aes-encryption-decryption

0
Best Answer
-
Hello,
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: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;
0
Answers
-
Hello,
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: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;
0 -
Hi @dreez thank you so much for your help.
by the way for padding PKCS7, is it consider the same like NoPadding?0 -
No it is not the same, but you can use no padding if you want.
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 results0 -
hi @dreez , i tried using None and '', but it doesnt work.
what should i put as nopadding?0 -
You can just ommit setting the padding, then there will be none padding.
'None' should work aswell, why does not it work for you? Do you get an error or what?0 -
Hi @dreez here is my error message if put "RijndaelCryptography.SetPaddingMode('None');"
you are right, once i remove the padding it works.
thank you so much Dreez !!0 -
Oh, I was wrong. Apparently PKCS7 is a default padding as I just read in documentation. Meaning that if you won't set any padding, the PKCS7 will be taken.
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.0 -
no worries. i just commented the padding.
and it works
thanks a lot0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions