/* Medium example for ESP8266 (not for Arduino, uses additional Base64 layer) */
#include "AESLib.h"
AESLib aesLib;
String plaintext = "HELLO WORLD!";
char cleartext[256];
char ciphertext[512];
// AES Encryption Key
byte aes_key[] = { 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 };
// General initialization vector (you must use your own IV's in production for full security!!!)
byte aes_iv[N_BLOCK] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
String encrypt_impl(char * msg, byte iv[]) {
int msgLen = strlen(msg);
char encrypted[2 * msgLen] = {0};
aesLib.encrypt64((const byte*)msg, msgLen, encrypted, aes_key, sizeof(aes_key), iv);
return String(encrypted);
}
String decrypt_impl(char * msg, byte iv[]) {
int msgLen = strlen(msg);
char decrypted[msgLen] = {0}; // half may be enough
aesLib.decrypt64(msg, msgLen, (byte*)decrypted, aes_key, sizeof(aes_key), iv);
return String(decrypted);
}
// Generate IV (once)
void aes_init() {
Serial.println("gen_iv()");
aesLib.gen_iv(aes_iv);
Serial.println("encrypt_impl()");
Serial.println(encrypt_impl(strdup(plaintext.c_str()), aes_iv));
}
void setup() {
Serial.begin(9600);
while (!Serial); // wait for serial port
delay(2000);
aes_init();
aesLib.set_paddingmode(paddingMode::CMS);
//
// verify with https://cryptii.com
// previously: verify with https://gchq.github.io/CyberChef/#recipe=To_Base64('A-Za-z0-9%2B/%3D')
//
char b64in[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
char b64out[base64_enc_len(sizeof(aes_iv))];
base64_encode(b64out, b64in, 16);
char b64enc[base64_enc_len(10)];
base64_encode(b64enc, (char*) "0123456789", 10);
char b64dec[ base64_dec_len(b64enc, sizeof(b64enc))];
base64_decode(b64dec, b64enc, sizeof(b64enc));
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;
byte enc_iv[N_BLOCK] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // iv_block gets written to, provide own fresh copy...
byte dec_iv[N_BLOCK] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
void loop() {
if (Serial.available()) {
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
String encrypted = encrypt_impl(cleartext, enc_iv);
sprintf(ciphertext, "%s", encrypted.c_str());
Serial.print("Ciphertext: ");
Serial.println(encrypted);
delay(1000);
// Decrypt
delay(1000);
String decrypted = decrypt_impl( ciphertext, dec_iv);
Serial.print("Cleartext: ");
Serial.println(decrypted);
if (decrypted.equals(cleartext)) {
Serial.println("SUCCES");
}
else
{
Serial.println("FAILURE");
}
for (int i = 0; i < 16; i++) {
enc_iv[i] = 0;
dec_iv[i] = 0;
}
}
}