I was working on an IoT device which I need to communicate with the device using Bluetooth. I have paired the device and I am trying to send Hexa value to the hardware and I have used AES encryption and decryption. I have mentioned the code which I used to encryption 1. The AES-128 is fixed as: 3A 60 43 2A 5C 01 21 1F 29 1E 0F 4E 0C 13 28 25 2. Ciphertext: 06 01 01 01 2D 1A 68 3D 48 27 1A 18 31 6E 47 1A
private static byte[] encrypt(byte[] sSrc, byte[] sKey) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(sKey, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(sSrc);
return encrypted;
}
Expected output is: ec 72 21 4c b0 9c d1 1b 52 fe 73 e9 01 d8 dc 48 but i am getting Decimals from signed twos's complemnet of the output like: -20, 114, 33, 76, -80, -100, -47, 27, 82, -2, 115, -23, 1, -40, -36, 72
and same like in the decryption 1. The AES-128 is fixed as: 3A 60 43 2A 5C 01 21 1F 29 1E 0F 4E 0C 13 28 25 2. Encrypted value: A5 6C 7D 75 48 DE FF EF E7 AC 1E A9 BC CE 66 E6 Code used for decryption
private static byte[] decrypt(byte[] sSrc, byte[] sKey) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(sKey, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = cipher.doFinal(sSrc);
return decrypted;
}
expected output is: 06 02 07 5B B 71 E7 01 01 00 05 0B 86 20 18 0A but i am getting Decimals from signed twos's complemnet of the output like: 6, 2, 7, 91, -70, 113, -25, 1, 1, 3, 5, 11, -122, 32, 24, 10 Please help me to sort out this issue, i have to get the out put as a hexadecimal value not in decimal form signed two's complement.