2

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     
2
  • When displaying binary data it is customery and more easily understood in hexadecimal format. Commented Jul 12, 2016 at 18:24
  • Think of "hex" as guerrilla and "decimal" as minion. ;-) Commented Jul 13, 2016 at 16:39

1 Answer 1

1

The Java key is specified by a string, the Arduino key by an integer array.

The Java key : static String encryptionKey = "0123456789123456";
In hex: 30313233 34353637 38393132 33343536

The Arduino key: uint8_t key[] = {0,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6};
In hex: 00010203 04050607 08090102 03040506 <— does not match the Java key

Change the Arduino key to: uint8_t key[] = {"0123456789123456"];
or: uint8_t key[] = {'0','1','2','3','4','5','6','7','8','9','1','2','3','4','5','6'};

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry for that , I copied it wrong.It was 16 bytes. My question is how i can sync? both of them to result in same cipher.
Yes it worked , thanks a ton !! Much appreciated . Sorry for being late. Just tested.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.