suculent / thinx-aes-lib

AES wrapper for ESP8266/ESP32/Arduino/nRF5x
Other
113 stars 39 forks source link

Decryption does not work #24

Closed lim-jeongmin closed 4 years ago

lim-jeongmin commented 4 years ago

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

image

chim3y commented 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?

suculent commented 4 years ago

See https://github.com/suculent/thinx-aes-lib/issues/18 what padding do you use?

    1. 2020 v 7:36, chim3y notifications@github.com:

@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.

ElMohamed commented 4 years ago

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() {

}

suculent commented 4 years ago

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.

    1. 2020 v 15:30, ElMohamed notifications@github.com:

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.

ElMohamed commented 4 years ago

Yes, indeed.

ElMohamed commented 4 years ago

@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().

chim3y commented 4 years ago

@ElMohamed @suculent thank you much for your time and suggestions...it finally worked.

ElMohamed commented 4 years ago

Goed no problem