arduino-esp8266-aes-lib
arduino-esp8266-aes-lib copied to clipboard
Encryption and decryption issue
Hi, bro, i used your lib to encrypt a msg first and then decrypt it, finding that part of the msg are not decrypted correctly.
Output:
Booting...
Let's encrypt:
IV b64: MDAwMDAwMDAwMDAwMDAwMA==
Message: {"data":{"value":300}, "SEQN":700 , "msg":"IT WORKS!!" }
Message in B64: eyJkYXRhIjp7InZhbHVlIjozMDB9LCAiU0VRTiI6NzAwICwgIm1zZyI6IklUIFdPUktTISEiIH0=
The lenght is: 76
Encryption done!
Cipher size: 80
Encrypted data in base64: Jdq1ko6TKPewmlSdn3zTaEP2yGWw10UXdansMHHuYbZdSDFWWH5cTq6LZO5Qey1JuY2ppygYicILzp8haWSS75oigJSZZo9uPXxuTwEO2mQ=
Done...
Plain in B64: 蟢��6D&.HxP�<bHVlIjozMDB9LCAiU0VRTiI6NzAwICwgIm1zZyI6IklUIFdPUktTISEiIH0
Plain size: 80
Decrypted data in base64: 﨩����悄�縧ue":300}, "SEQN":700 , "msg":"IT WORKS!!" }���
Code:
#include "AES.h"
#include "base64.h"
AES aes;
void setup() {
Serial.begin(115200);
Serial.println("\nBooting...");
char b64data[200];
byte cipher[1000];
byte iv [N_BLOCK] ;
Serial.println("Let's encrypt:");
// Our AES key. Note that is the same that is used on the Node-Js side but as hex bytes.
byte key[] = { 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 };
// The unitialized Initialization vector
//byte my_iv[N_BLOCK] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
byte my_iv[N_BLOCK] = { 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 };
// Our message to encrypt. Static for this example.
String msg = "{\"data\":{\"value\":300}, \"SEQN\":700 , \"msg\":\"IT WORKS!!\" }";
aes.set_key( key , sizeof(key)); // Get the globally defined key
// Print the IV
base64_encode( b64data, (char *)my_iv, N_BLOCK);
Serial.println(" IV b64: " + String(b64data));
Serial.println(" Message: " + msg );
int b64len = base64_encode(b64data, (char *)msg.c_str(), msg.length());
Serial.println (" Message in B64: " + String(b64data) );
Serial.println (" The lenght is: " + String(b64len) );
// For sanity check purpose
//base64_decode( decoded , b64data , b64len );
//Serial.println("Decoded: " + String(decoded));
// Encrypt! With AES128, our key and IV, CBC and pkcs7 padding
aes.do_aes_encrypt((byte *)b64data, b64len , cipher, key, 128, my_iv);
Serial.println("Encryption done!");
Serial.println("Cipher size: " + String(aes.get_size()));
b64len = base64_encode(b64data, (char *)cipher, aes.get_size() );
Serial.println ("Encrypted data in base64: " + String(b64data) );
Serial.println("Done...");
byte plain[1000];
b64len = base64_decode(b64data, b64data, b64len);
aes.do_aes_decrypt((byte *)b64data, b64len, plain, key, 128, my_iv);
base64_decode(b64data, (char *)plain, aes.get_size());
Serial.println (" Plain in B64: " + String((char *)plain) );
Serial.println(" Plain size: " + String(aes.get_size()));
Serial.println (" Decrypted data in base64: " + String(b64data) );
}
void loop() {
// put your main code here, to run repeatedly:
}
Yah sorry decrypt is broken
On Thu, Mar 23, 2017 at 8:42 PM Adon [email protected] wrote:
Hi, bro, i used your lib to encrypt a msg first and then decrypt it, finding that part of the msg are not decrypted correctly.
Output:
Booting... Let's encrypt: IV b64: MDAwMDAwMDAwMDAwMDAwMA== Message: {"data":{"value":300}, "SEQN":700 , "msg":"IT WORKS!!" } Message in B64: eyJkYXRhIjp7InZhbHVlIjozMDB9LCAiU0VRTiI6NzAwICwgIm1zZyI6IklUIFdPUktTISEiIH0= The lenght is: 76 Encryption done! Cipher size: 80 Encrypted data in base64: Jdq1ko6TKPewmlSdn3zTaEP2yGWw10UXdansMHHuYbZdSDFWWH5cTq6LZO5Qey1JuY2ppygYicILzp8haWSS75oigJSZZo9uPXxuTwEO2mQ= Done... Plain in B64: 蟢���6D&.HxP�<bHVlIjozMDB9LCAiU0VRTiI6NzAwICwgIm1zZyI6IklUIFdPUktTISEiIH0����� Plain size: 80 Decrypted data in base64: 﨩�����悄�縧ue":300}, "SEQN":700 , "msg":"IT WORKS!!" }���
Code:
#include "AES.h" #include "base64.h"
AES aes;
void setup() { Serial.begin(115200); Serial.println("\nBooting...");
char b64data[200]; byte cipher[1000]; byte iv [N_BLOCK] ;
Serial.println("Let's encrypt:"); // Our AES key. Note that is the same that is used on the Node-Js side but as hex bytes. byte key[] = { 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 };
// The unitialized Initialization vector //byte my_iv[N_BLOCK] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; byte my_iv[N_BLOCK] = { 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 };
// Our message to encrypt. Static for this example. String msg = "{"data":{"value":300}, "SEQN":700 , "msg":"IT WORKS!!" }";
aes.set_key( key , sizeof(key)); // Get the globally defined key
// Print the IV base64_encode( b64data, (char *)my_iv, N_BLOCK); Serial.println(" IV b64: " + String(b64data));
Serial.println(" Message: " + msg );
int b64len = base64_encode(b64data, (char *)msg.c_str(), msg.length()); Serial.println (" Message in B64: " + String(b64data) ); Serial.println (" The lenght is: " + String(b64len) );
// For sanity check purpose //base64_decode( decoded , b64data , b64len ); //Serial.println("Decoded: " + String(decoded));
// Encrypt! With AES128, our key and IV, CBC and pkcs7 padding aes.do_aes_encrypt((byte *)b64data, b64len , cipher, key, 128, my_iv);
Serial.println("Encryption done!");
Serial.println("Cipher size: " + String(aes.get_size()));
b64len = base64_encode(b64data, (char *)cipher, aes.get_size() ); Serial.println ("Encrypted data in base64: " + String(b64data) );
Serial.println("Done...");
byte plain[1000]; b64len = base64_decode(b64data, b64data, b64len); aes.do_aes_decrypt((byte *)b64data, b64len, plain, key, 128, my_iv);
base64_decode(b64data, (char *)plain, aes.get_size()); Serial.println (" Plain in B64: " + String((char *)plain) ); Serial.println(" Plain size: " + String(aes.get_size())); Serial.println (" Decrypted data in base64: " + String(b64data) ); }
void loop() { // put your main code here, to run repeatedly: }
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/kakopappa/arduino-esp8266-aes-lib/issues/1, or mute the thread https://github.com/notifications/unsubscribe-auth/AHIM5oBtkNqxyvhwkGr5cPskl36rKSguks5ronazgaJpZM4Mmwqu .
thanks PS: i also tried some other online aes encryption tools but return a different result.
You need to base64 decode the IV.
Heres a working decoder lib
edit the AESLib.cpp file with this
String AESLib::decrypt(String msg, byte key[], String my_iv) {
aes.set_key(key, sizeof(key));
char data_decoded[200];
char iv_decoded[200];
byte out[200];
char temp[200];
msg.toCharArray(temp, 200);
int b64len = base64_decode(data_decoded, temp, msg.length());
my_iv.toCharArray(temp, 200);
base64_decode(iv_decoded, temp, my_iv.length());
aes.do_aes_decrypt((byte *)data_decoded, b64len, out, key, 128, (byte *)iv_decoded);
char message[msg.length()];
base64_decode(message, (char *)out, msg.length());
return String(message);
}
and in AESLib.h
String decrypt(String msg, byte key[], String my_iv);
and then call it with
String Msg = aesLib.decrypt(String(encmsg), key, String(my_iv));
Are you planning on fixing the decryption part?
did you try @mtnbrit decrypt code above?
I did, but it looks like there are still an issue with the width.
Hello, I am not able to put String AESLib :: decrypt (String msg, byte key [], String my_iv) {to work, this gives me this error: I would like to get a call because this lib and 5 * to use is missing even this

I would not use the String object. Rather use a char array buffer with enough width in it and then terminate it, where needed.
Edit:
Take a look at this lib and example.
https://github.com/intrbiz/arduino-crypto/issues/6
This should help
'base64_encode' was not declared in this scope plzz help me out
Thanks