I'm a little confused on what configuration I should use on the two libraries I am using from programming an Arduino and Android. I want to be able to:
- Create random string in Arduino
- Pass it to Android to encrypt with AES and shared secret key
- Pass back to Arduino to decrypt with AES and shared secret key
- Verify that original random string matches decrypted string
I am using this Arduino library and this Android library
My arduino code I set up to test encrypting an decrypting:
#include <AES.h>
AES aes ;
#define KEYLENGTH 32
char PassString[] = "This is hard to believe but true";
byte key[KEYLENGTH];
char Message[] = "Ncrypted Message";
byte plain[N_BLOCK];
byte cipher [N_BLOCK] ;
byte decrypted [N_BLOCK] ;
void setup() {
Serial.begin(9600);
Serial.println(F("Starting AES test"));
// Pass the key into the byte array
for (int i = 0; i < KEYLENGTH; i++) {
key[i] = PassString[i];
}
if (aes.set_key (key, KEYLENGTH) !=0){
Serial.println(F("Failed to set key"));
}
}
void loop() {
// Show original message
Serial.print(F("Un-encrypted message: "));
Serial.println(Message);
Serial.println(F("Un-encrypted binary: "));
for (int i = 0; i < N_BLOCK; i++) {
plain[i] = Message[i];
cipher[i] = 0;
decrypted[i] = 0;
Serial.print(plain[i]);
Serial.print(F(" "));
}
Serial.println(F(""));
// Show encrypted message
if (aes.encrypt(plain, cipher) == 0) {
Serial.println(F("encrypted: "));
for (int i = 0; i < N_BLOCK; i++) {
Serial.print(cipher[i]);
Serial.print(F(" "));
}
Serial.println(F(""));
} else {
Serial.println(F("Failed to encrypt"));
}
// Show decrypted message
if (aes.decrypt(cipher, decrypted) == 0) {
Serial.println(F("decrypted binary: "));
for (int i = 0; i < N_BLOCK; i++) {
Serial.print(decrypted[i]);
Serial.print(F(" "));
}
Serial.println(F(""));
Serial.println(F("decrypted char: "));
for (int i = 0; i < N_BLOCK; i++) {
Serial.print(char(decrypted[i]));
}
Serial.println(F(""));
} else {
Serial.println(F("Failed to decrypt"));
}
Serial.println(F("AES test complete"));
delay(100000);
}
My Android program is almost verbatim from the link above.
I can get both of them to encrypt and decrypt their own messages, but their set ups seem very different and haven't been able to encrypt each others. The Android code seems to have quite a bit more involved like creating keys and salts. Both libraries are pretty versatile and I'm not sure how to make them encrypt the same way.
I seem to be missing some basics concepts, but after all the researching I've done on encryption I've realized that this is one of the most confusing and difficult parts of my project! A push in the right direction would be very much appreciated.