Specify Cipher.getInstance() arguments?
android, encryption, java
Solution
Another 'short answer', but I believe AES-GCM is more secure that CBC mode and been around for a couple of years however if you want to use in Android you'll need to include spongycastle
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
Problem
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