0
votes

Using AES library i am trying to send encrypted data from arduino side to raspberry pi side.The encrypted data that is being printed on the arduino serial monitor is not the same as what is being printed on the raspberry side. Maybe it is the decoding problem.

Also while decrypting on the raspberry pi side it gives an error saying "the input text must be multiple of 16 in length", when i pad the input( temperature data) with zeroes it still gives the same error message.

I have tried using 'utf-8' and 'iso-8859-1' for decoding but still it doesnt show the same decrypted data.

PYTHON CODE :

 from Crypto.Cipher import AES
 ser=serial.Serial(' /dev/ttyS0',9600)
 st=ser.readline()
 st1=st.decode('utf-8')
 obj = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
 ciphertext = obj.encrypt(message)

 obj2 = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
 obj2.decrypt(ciphertext)

ARDUINO CODE :

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

byte iv [N_BLOCK] ;
int plainPaddedLength = sizeof(chartemp) + (N_BLOCK - ((sizeof(chartemp)-1) % 16)); 
byte cipher [plainPaddedLength];
byte check [plainPaddedLength]; 


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

aes.do_aes_encrypt(chartemp,sizeof(chartemp),cipher,key,bits,iv);


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


aes.printArray(cipher,(bool)false); //print cipher with padding
String cipher1=String((char*)cipher);

myserial.println(cipher1);

}

HERE chartemp is the temperature that from LM35 IC converted to characted array.

I expect the output on the raspberry pi side to be decrypted properly

1
I tried using base64 too, it didn't show the expected result. Please help - Shakir

1 Answers

0
votes

Encrypted data is a sequence of pseudo-random bytes. It is not a valid UTF-8 string.

This line is a bit dodgy, but probably technically "works:"

String cipher1=String((char*)cipher);

But this line is incorrect:

st1=st.decode('utf-8')

You can't take random data and decode it as utf-8. You either need to send and receive the data as just a string of bytes, or encode the data into a string, such as with Base64. I suspect you'll be more comfortable with the latter, so look at Base64 in Java and base64 in Python.