Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- byte aes_key[] = {0xA6, 0x39, 0x6, 0xE0, 0x35, 0x8C, 0x18, 0x7D, 0xDC, 0xF5, 0xB8, 0x2D, 0xA5, 0x83, 0x76, 0x46, 0xF0, 0x28, 0xF3, 0x988};
- byte aes_iv[] = {0x48, 0x6C, 0x69, 0xCA, 0x8F, 0x66, 0xAF, 0x1B, 0xAD, 0x30, 0x1B, 0xE6, 0x51, 0xEA, 0x08, 0x89, 0xE4, 0xF4, 0x71, 0xCF, 0x00, 0xC5, 0xDC, 0x3C, 0x33, 0x0B, 0xCE, 0xD7, 0x6A, 0x8C, 0x97, 0xCF};
- void aes_init() {
- aesLib.gen_iv(aes_iv);
- aesLib.set_paddingmode((paddingMode)0);
- }
- String encrypt(char * msg, byte iv[]) {
- unsigned long ms = micros();
- int msgLen = strlen(msg);
- char encrypted[2 * msgLen];
- aesLib.encrypt64(msg, msgLen, encrypted, aes_key, sizeof(aes_key), iv);
- // Serial.print("Encryption took: ");
- // Serial.print(micros() - ms);
- // Serial.println("us");
- 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, aes_key, sizeof(aes_key), iv);
- // Serial.print("Decryption [2] took: ");
- // Serial.print(micros() - ms);
- // Serial.println("us");
- return String(decrypted);
- }
- void setup() {
- aes_init();
- String encrypted = encrypt((const char*)"Rian Pratama", aes_iv);
- String decrypted = decrypt((char*)encrypted.c_str(), aes_iv);
- Serial.print("Encrypt: ");
- Serial.println(encrypted);
- Serial.print("Decrypt: ");
- Serial.println(decrypted);
- String plain = encrypted;
- // decryped may contain mess if not properly padded
- if (decrypted.indexOf(plain) == -1) {
- Serial.println("Decryption FAILED!");
- Serial.print("At:");
- Serial.println(plain.indexOf(decrypted));
- delay(5000);
- } else {
- if (plain.length() == decrypted.length()) {
- Serial.println("Decryption successful.");
- } else {
- Serial.print("Decryption length incorrect. Plain: ");
- Serial.print(plain.length());
- Serial.print(" dec: ");
- Serial.println(decrypted.length());
- }
- }
- }
- void loop(){}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement