Closed lim-jeongmin closed 4 years ago
@lim-jeongmin i also experienced the same problem. AESlib generated different ciphertext as opposed to the one (https://www.devglan.com/online-tools/aes-encryption-decryption) and hence it decrypts differently.
Please, can you kindly suggest how did you fix the problem?
See https://github.com/suculent/thinx-aes-lib/issues/18 what padding do you use?
@lim-jeongmin i also experienced the same problem. AESlib generated different ciphertext as opposed to the one (https://www.devglan.com/online-tools/aes-encryption-decryption) and hence it decrypts differently.
Please, can you kindly suggest how did you fix the problem?
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.
The array my_iv was overwritten by encryption, so you can not use my_iv again for decryption. Try this for example:
AESLib aesLib;
String text = "i am happy";
char cleartext[256]; char ciphertext[512];
// AES Encryption Key byte key[] = { 0x15, 0x2B, 0x7E, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C };
// General initialization vector (you must use your own IV's in production for full security!!!) byte my_iv[N_BLOCK] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
String encrypt(char msg, byte iv[]) { int msgLen = strlen(msg); char encrypted[2 msgLen]; aesLib.encrypt64(msg, msgLen, encrypted, key, sizeof(key), iv); return String(encrypted); }
String decrypt(char * msg, byte iv[]) { unsigned long ms = micros(); int msgLen = strlen(msg); char decrypted[msgLen]; // half may be enough aesLib.decrypt64(msg, msgLen, decrypted, key, sizeof(key), iv); return String(decrypted); }
void setup() { Serial.begin(115200); //aesLib.gen_iv(my_iv); byte my_iv1[N_BLOCK] = { 0x15, 0x2B, 0x7E, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }; text.toCharArray(cleartext, 256); // Encrypt String encrypted = encrypt(cleartext, my_iv1); Serial.print("Ciphertext: "); Serial.println(encrypted); sprintf(ciphertext, "%s", encrypted.c_str());
byte my_iv2[N_BLOCK] = { 0x15, 0x2B, 0x7E, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }; // Decrypt String decrypted = decrypt(ciphertext, my_iv2);
if(decrypted.length()!=0){ Serial.print("Cleartext: "); Serial.println(decrypted); } else Serial.print("Cleartext: "); }
void loop() {
}
Of course. The iv gets changed with each block so you need to save the INITIAL value to decode the chain of blocks.
That’s how block cipher works.
M.
The array my_iv was overwritten by encryption, so you can not use my_iv again for decryption. Try this for example:
include "AESLib.h"
AESLib aesLib;
String text = "i am happy";
char cleartext[256]; char ciphertext[512];
// AES Encryption Key byte key[] = { 0x15, 0x2B, 0x7E, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C };
// General initialization vector (you must use your own IV's in production for full security!!!) byte my_iv[N_BLOCK] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
String encrypt(char msg, byte iv[]) { int msgLen = strlen(msg); char encrypted[2 msgLen]; aesLib.encrypt64(msg, msgLen, encrypted, key, sizeof(key), iv); return String(encrypted); }
String decrypt(char * msg, byte iv[]) { unsigned long ms = micros(); int msgLen = strlen(msg); char decrypted[msgLen]; // half may be enough aesLib.decrypt64(msg, msgLen, decrypted, key, sizeof(key), iv); return String(decrypted); }
void setup() { Serial.begin(115200); //aesLib.gen_iv(my_iv); byte my_iv1[N_BLOCK] = { 0x15, 0x2B, 0x7E, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }; text.toCharArray(cleartext, 256); // Encrypt String encrypted = encrypt(cleartext, my_iv1); Serial.print("Ciphertext: "); Serial.println(encrypted); sprintf(ciphertext, "%s", encrypted.c_str());
byte my_iv2[N_BLOCK] = { 0x15, 0x2B, 0x7E, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }; // Decrypt String decrypted = decrypt(ciphertext, my_iv2);
if(decrypted.length()!=0){ Serial.print("Cleartext: "); Serial.println(decrypted); } else Serial.print("Cleartext: "); }
void loop() {
}
— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.
Yes, indeed.
@chim3y. Do you use AesLib::decrypt64 function if so at the python side you need to do the following: encod64(plaintext) - > encrypt(encoded64plaintex) - > encode64(encryptedEncode64plaintext)
Or you need to use AesLib:encrypt().
@ElMohamed @suculent thank you much for your time and suggestions...it finally worked.
Goed no problem
include "AESLib.h"
AESLib aesLib;
String text = "i am happy";
char cleartext[256]; char ciphertext[512];
// AES Encryption Key byte key[] = { 0x15, 0x2B, 0x7E, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C };
// General initialization vector (you must use your own IV's in production for full security!!!) byte my_iv[N_BLOCK] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
String encrypt(char msg, byte iv[]) {
int msgLen = strlen(msg); char encrypted[2 msgLen]; aesLib.encrypt64(msg, msgLen, encrypted, key, sizeof(key), iv);
return String(encrypted); }
String decrypt(char * msg, byte iv[]) { unsigned long ms = micros(); int msgLen = strlen(msg); char decrypted[msgLen]; // half may be enough aesLib.decrypt64(msg, msgLen, decrypted, key, sizeof(key), iv);
return String(decrypted); }
void setup() { Serial.begin(115200); aesLib.gen_iv(my_iv);
text.toCharArray(cleartext, 256); // Encrypt String encrypted = encrypt(cleartext, my_iv); Serial.print("Ciphertext: "); Serial.println(encrypted); sprintf(ciphertext, "%s", encrypted.c_str());
// Decrypt String decrypted = decrypt(ciphertext, my_iv);
if(decrypted.length()!=0){ Serial.print("Cleartext: "); Serial.println(decrypted);
} else Serial.print("Cleartext: "); }
void loop() {
}
I tried with a little variation, it is not decrypted