I am trying to send a password from Android to Arduino using AES Algo. I am facing issues with the generated key difference in both encrypted implementations. I am sure there is some configuration sync. I searched internet found some strings and tried all. But still my keys/cipher doesn't match.
Any inputs are welcomed .
Arduino code:
void setup()
{
Serial.begin(57600);
}
void loop()
{
int i = 0;
uint8_t key[] = {0,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6};
char data[] = "sanmacs88 "; //16 chars == 16 bytes
aes128_enc_single(key, data);
Serial.println("encrypted:");
for(i=0;i<16;i++)
Serial.println((int)data[i]);
aes128_dec_single(key, data);
Serial.println("decrypted:");
Serial.println(data);
delay(10000);
}
Output : 75 45 -7 78 89 123 1 96 -10 36 110 -105 -119 -11 -7 -8 decrypted: sanmacs88
Java Code :
public class AES {
static String IV = "AAAAAAAAAAAAAAAA";
static String plaintext = "sanmacs88 "; /*Note null padding*/
static String encryptionKey = "0123456789123456";
public static void main(String [] args) {
try {
System.out.println("==Java==");
System.out.println("plain: " + plaintext);
byte[] cipher = encrypt(plaintext, encryptionKey);
System.out.print("cipher: ");
for (int i=0; i<cipher.length; i++)
System.out.print(new Integer(cipher[i])+" ");
System.out.println("");
String decrypted = decrypt(cipher, encryptionKey);
System.out.println("decrypt: " + decrypted);
} catch (Exception e) {
e.printStackTrace();
}
}
public static byte[] encrypt(String plainText, String encryptionKey) throws Exception {
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding", "SunJCE");
SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(plainText.getBytes("UTF-8"));
}
public static String decrypt(byte[] cipherText, String encryptionKey) throws Exception{
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding", "SunJCE");
SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
cipher.init(Cipher.DECRYPT_MODE, key);
return new String(cipher.doFinal(cipherText),"UTF-8");
}
}
Output:
plain: sanmacs88
cipher: 38 60 69 -111 -44 115 -84 -118 72 -124 86 69 -61 87 -20 63
decrypt: sanmacs88