0

I'm trying to encrypt a JSON string in Android and decrypt it in Ruby using AESCrypt.

AESCrypt.decrypt(dataToDecrypt, secret)

With this Java code I could decrypt second half of the data!

MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(secret.getBytes("UTF-8"));
byte[] digest = md.digest();

SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = null;
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, newKey);
byte[] encryptedData = cipher.doFinal(textBytes);

String encryptedDataStr = Base64.encodeToString(encryptedData, Base64.DEFAULT)

Raw data is:

{"device_id":"863438021956196","imei":"863438021956196"}

And decrypted data in Ruby is:

\xEE\x99\x95\xC5p\x17\x8A\xFB\xF0\xA5\xC7\x1D7\x98\xBD\xD93438021956196\",\"imei\":\"863438021956196\"}

What is the problem?

4

2 回答 2

1

CBC 模式需要 IV。如果没有明确指定一个,您就依赖于默认值。看起来默认的 Java IV 与 Ruby 代码的默认 IV 不同。不要使用默认值。

于 2014-09-27T12:20:19.437 回答
0

I had this same issue, as @GreyS mentions the default IV's are different, AFAIK Java creates a random IV if you don't supply one. Also pretty sure AESCrypt uses PKCS7Padding. In any case, to help to work between AESCrypt and Android I recently created AESCrypt-Android

于 2014-10-05T07:33:35.310 回答