0

I have an Android app that encrypts data using AES with ECB and ZeroBytePadding. Everything works fine in that environment: encrypted data gets decrypted in Android without a problem, as follows:

public static String encrypt(byte[] raw, byte[] clear) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES/ECB/ZeroBytePadding");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

    byte[] encrypted = cipher.doFinal(clear);
    return android.util.Base64.encodeToString(encrypted, android.util.Base64.NO_WRAP);
}

public static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES/ECB/ZeroBytePadding");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec);

    byte[] decrypted = cipher.doFinal(encrypted);
    return decrypted;
}

However, I recently decided to decrypt the data in a Web app and when I tried to use the same decrypt() method, Cipher.getInstance("AES/ECB/ZeroBytePadding") threw an exception:

java.security.NoSuchAlgorithmException: Cannot find any provider supporting     
AES/ECB/ZeroBytePadding

I assume that some Android library is providing a suitable cipher provider that is missing from javax.crypto.Cipher. Has anyone else had this problem or know what I can do about it? Changing the cipher padding to PKCS5PADDING is not an option, due to the many messages that are already encrypted with the earlier options.

FractalBob
  • 3,225
  • 4
  • 29
  • 40
  • Since Android uses a modified version of Bouncy Castle, have you tried adding Bouncy Castle to your environment and using it as the `javax.crypto` provider? – CommonsWare Mar 03 '13 at 21:27
  • I didn't know that. So, I added bcprov-jdk150n-148.jar to my NetBeans library and imported org.bouncycastle.jcajce.provider.symmetric.* into the Java file, but I'm still getting the same error. I'm still using javax.crypto.Cipher; is there an equivalent in BouncyCastle? – FractalBob Mar 04 '13 at 04:35

1 Answers1

0

Actually there is really not such an algorithm. Look at official java docs here http://docs.oracle.com/javase/7/docs/api/javax/crypto/Cipher.html

Yaya
  • 600
  • 1
  • 11
  • 28