spaniakos / AES

AES for microcontrollers (Arduino & Raspberry pi)
http://spaniakos.github.io/AES/
GNU Affero General Public License v3.0
126 stars 55 forks source link

encryption in arduino nano and problem in decrypt data in other arduino nano ( data sent by RF communication) #36

Closed vamsikrishnaA9 closed 4 years ago

vamsikrishnaA9 commented 4 years ago

hi @spaniakos. i want to send data through one arduino nano to other nano by NRF24L01 module. i successfully sent the encrypted data to other nano. But the problem is at aes.do_aes_decrypt(cipher, aes.get_size(), check, key, bits, iv); the above 'check' wont giving expected decrypted result. here are the results observed in arduino software.

img here is the code written on both tx and rx side

tx:

include

include "./printf.h"

include

include

include

RF24 radio(9, 10); // CE, CSN AES aes ; const byte address[6] = "00001"; unsigned int keyLength [3] = {128, 192, 256}; // key length: 128b, 192b or 256b

byte key = (unsigned char)"01234567890123456789012345678901"; // encryption key byte plain[] = "hello"; // plaintext to encrypt

unsigned long long int myIv = 36753562; int i = 0; void setup () { Serial.begin (9600) ; printf_begin(); radio.begin(); radio.openWritingPipe(address); radio.setPALevel(RF24_PA_MIN); radio.stopListening(); }

void loop () { while (i < 1) { aesTest(128); i++; } }

void aesTest (int bits) { aes.iv_inc();

byte iv [N_BLOCK] ; int plainPaddedLength = sizeof(plain) + (N_BLOCK - ((sizeof(plain) - 1) % 16)); // length of padded plaintext [B]

byte cipher [plainPaddedLength]; // ciphertext (encrypted plaintext) byte check [plainPaddedLength]; // decrypted plaintext

aes.set_IV(myIv); aes.get_IV(iv);

aes.do_aes_encrypt(plain, sizeof(plain), cipher, key, bits, iv); Serial.print("size of encrytped cipher: "); Serial.println(sizeof(cipher)); // Serial.println(byte(cipher));

Serial.println("printing encrypted data byte values"); for(int i=0;i<sizeof(cipher);i++){ Serial.print(cipher[i]); } Serial.println(); radio.write(&cipher, sizeof(cipher));

aes.set_IV(myIv); aes.get_IV(iv); Serial.print("IV:"); for (int i = 0; i < sizeof(iv); i++) { // Serial.print(iv[i]); printf_P(PSTR("%02x"),iv[i]); } Serial.println();

aes.do_aes_decrypt(cipher, aes.get_size(), check, key, bits, iv); Serial.print("printing decrypted check values:"); for(int i =0;i<sizeof(check);i++){ Serial.print(check[i]); }

Serial.println(); Serial.print("- plain: "); aes.printArray(plain, (bool)true); //print plain with no padding

Serial.print("- cipher: "); aes.printArray(cipher, (bool)false); //print cipher with padding

Serial.print("- check: "); aes.printArray(check, (bool)true); //print decrypted plain with no padding

Serial.print("- iv: "); aes.printArray(iv, 16); //print iv printf("\n===================================================================================\n"); }

RX:

include

include "./printf.h"

include

include

include

RF24 radio(9, 10); // CE, CSN AES aes ; const byte address[6] = "00001"; unsigned int keyLength [3] = {128, 192, 256}; // key length: 128b, 192b or 256b byte key = (unsigned char)"01234567890123456789012345678901"; // encryption key byte plain[] = "hello"; //plain text char text[17] = ""; unsigned long long int myIv = 36753562; // CBC initialization vector; real iv = iv x2 ex: 01234567 = 0123456701234567 int i = 0; void setup () { Serial.begin (9600) ; printf_begin(); radio.begin(); radio.openReadingPipe(0, address); radio.setPALevel(RF24_PA_MIN); radio.startListening(); }

void loop () {

if (radio.available()) { char text[17] = ""; radio.read(&text, sizeof(text)); Serial.print("recieved encrypted byte data"); Serial.println(); for (int i = 0; i < sizeof(text); i++) {
Serial.print((byte)text[i]); } Serial.println(); Serial.print("size of recieved encrytped data: "); Serial.println(sizeof(text));
// Serial.println(); aesTest(128);

} }

void aesTest (int bits) { aes.iv_inc();

byte iv [N_BLOCK] ; int plainPaddedLength = sizeof(plain) + (N_BLOCK - ((sizeof(plain) - 1) % 16)); // length of padded plaintext [B] byte cipher [plainPaddedLength]; // ciphertext (encrypted plaintext) byte check [plainPaddedLength]; // decrypted plaintext

aes.set_IV(myIv); aes.get_IV(iv); Serial.print("IV:"); for (int i = 0; i < sizeof(iv); i++) { // Serial.print(iv[i]); printf_P(PSTR("%02x"), iv[i]); } Serial.println();

aes.set_IV(myIv); aes.get_IV(iv);

aes.do_aes_decrypt((byte)text, aes.get_size(), check, key, bits, iv); Serial.print("printing decrypted check values:"); for(int i =0;i<sizeof(check);i++){ Serial.print(check[i]); }

Serial.println(); Serial.print("- check: "); aes.printArray(check, (bool)true); //print decrypted plain with no padding

Serial.print("- iv: "); aes.printArray(iv, 16); //print iv printf("\n===================================================================================\n"); }

Any suggestions please. thank u.