I'm using the following in an android app and a standalone java app:
private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(clear);
...
I get different encrypted strings on android vs my standalone java app (both using the same code and key). I get the same exception (javax.crypto.BadPaddingException: Blocktype mismatch: 0) as in this question:
RSA Encryption: Difference between Java and Android
And the suggested solution is to specify the padding strategy like:
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
but I'm using "AES", not "RSA", and am not sure how to specify the padding in combination with AES. How would I construct the string passed to Cipher.getInstance() in that case? I gave this a try:
Cipher cipher = Cipher.getInstance("AES/PKCS1Padding");
but get an exception about that being invalid.
Thanks