Hello!
I use an AES lib from this source:
On a Mega2560
Have loaded an example code from a sketchbook for this library:
/* Minimalistic example for Readme */
#include "AESLib.h"
AESLib aesLib;
String plaintext = "24051984";
char cleartext[256];
char ciphertext[512];
// AES Encryption Key
byte aes_key[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7 };
// General initialization vector (you must use your own IV's in production for full security!!!)
byte aes_iv[N_BLOCK] = { 7, 6, 5, 4, 3, 2, 1, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
// Generate IV (once)
void aes_init() {
Serial.println("gen_iv()");
aesLib.gen_iv(aes_iv);
// workaround for incorrect B64 functionality on first run...
Serial.println("encrypt()");
Serial.println(encrypt(strdup(plaintext.c_str()), aes_iv));
}
String encrypt(char * msg, byte iv[]) {
int msgLen = strlen(msg);
Serial.print("msglen = "); Serial.println(msgLen);
char encrypted[4 * msgLen]; // AHA! needs to be large, 2x is not enough
aesLib.encrypt64(msg, encrypted, aes_key, iv);
Serial.print("encrypted = "); Serial.println(encrypted);
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, decrypted, aes_key, iv);
return String(decrypted);
}
void setup() {
Serial.begin(230400);
while (!Serial); // wait for serial port
delay(2000);
Serial.println("aes_init()");
aes_init();
Serial.println("Enter text to be encrypted into console (no feedback) and press ENTER (newline):");
}
/* non-blocking wait function */
void wait(unsigned long milliseconds) {
unsigned long timeout = millis() + milliseconds;
while (millis() < timeout) {
yield();
}
}
unsigned long loopcount = 0;
void loop() {
if (Serial.available() > 0) {
loopcount++; Serial.println(loopcount); // entry counter
String readBuffer = Serial.readStringUntil('\n');
Serial.println("INPUT:" + readBuffer);
sprintf(cleartext, "%s", readBuffer.c_str()); // must not exceed 255 bytes; may contain a newline
// Encrypt
byte enc_iv[N_BLOCK] = { 7, 6, 5, 4, 3, 2, 1, 9, 8, 7, 6, 5, 4, 3, 2, 1 }; // iv_block gets written to, provide own fresh copy...
String encrypted = encrypt(cleartext, enc_iv);
sprintf(ciphertext, "%s", encrypted.c_str());
Serial.print("Ciphertext: ");
Serial.println(encrypted);
}
}
Only what i changed is the: text to encrypt to: 24051984
Key: 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7
IV: 7, 6, 5, 4, 3, 2, 1, 9, 8, 7, 6, 5, 4, 3, 2, 1
The rest of the code is the normal example from the sketchbook.
I have try to decode the received base64 in my program that i make, but no success. Also, i tested the encoding/decoding with an online tool to encode . At a same page, i used to crate chain to decode with a revers procedure.
If i use a web based encode i can later decode it on my program, but the Arduino encoded base64 not. Also, i can't decode with an online tool and with my offline tool the Arduino encoded base64.
The arduino code is example provided from the library autor to show an aes-base64 encoding, and i also don't see the fault.
Can somebody show me what is wrong od correct the code, to be useful for sending an base64 encrypted aes ciphr that i can later descrypt with my keys and iv that i provided?
Something is wrong, but i don't known what?