I use the following in an Android app and standalone java application:
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 in android vs my standalone java application (both use the same code and key). I get the same exception (javax.crypto.BadPaddingException: label mismatch: 0), as in this question:
RSA Encryption: The Difference Between Java and Android
And the proposed solution is to indicate a filling strategy, for example:
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
but I use "AES" and not "RSA", and I donβt know how to specify an addition in combination with AES. How would I build the string passed to Cipher.getInstance (), in this case? I tried:
Cipher cipher = Cipher.getInstance("AES/PKCS1Padding");
but get an exception due to invalid.
thanks
source share