3

I receive an error while decrypting: (javax.crypto.BadPaddingException: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt)

My code encryption / decryption:

private static byte[] password = null; //  this.password = editText.getBytes();
static final byte[] ivBytes = {'6','g','6','o','d','a','0','u','4','n','w','i','6','9','i','j'};

public static byte[] encrypt(String text) throws Exception {
    byte[] clear = text.getBytes("UTF-8");
    IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);

    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
    sr.setSeed(password);
    kgen.init(256, sr); // 192 and 256 bits may not be available
    SecretKey skey = kgen.generateKey();
    byte[] key = skey.getEncoded();

    SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivSpec);
    byte[] encrypted = cipher.doFinal(clear);
    return encrypted;
}

public static String decrypt(byte[] encrypted) throws Exception {
    IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);

    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
    sr.setSeed(password);
    kgen.init(256, sr); // 192 and 256 bits may not be available
    SecretKey skey = kgen.generateKey();
    byte[] key = skey.getEncoded();

    SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivSpec);
    String decrypted = new String(cipher.doFinal(encrypted));
    return decrypted;
}

I suspect that the bug generateKey.

Charles
  • 50,943
  • 13
  • 104
  • 142
Stan
  • 155
  • 1
  • 6
  • possible duplicate of [Encryption error on Android 4.2](http://stackoverflow.com/questions/13383006/encryption-error-on-android-4-2) – ntoskrnl Mar 15 '14 at 18:29
  • On second thought, this question has a much better answer: http://stackoverflow.com/questions/13433529/android-4-2-broke-my-encrypt-decrypt-code-and-the-provided-solutions-dont-work?lq=1 – ntoskrnl Mar 15 '14 at 18:54

3 Answers3

2

You're doing two things wrong:

  • Generating a key from a password by using the key to seed a PRNG is a bad idea. Use password-based-encryption instead. Java has an implementation of PKCS#5 that will generate a key from a password.

  • You need to use a new strong-random IV for each encryption:

    • When you encrypt, don't specify an IV in cipher.init(). A new one will be generated for you.
    • encrypt() needs to serialise both the IV (cipher.getIV()) and the ciphertext into a byte array.
    • decrypt(): separate the IV from the ciphertext, build an IvParameterSpec from it and feed into cipher.init() as you currently do.
user3392484
  • 1,929
  • 9
  • 9
0

Your issues is that when you decrypt, you generate a new secret key instead of deriving it from password. Check out this blog post to see how password-based encryption has to be implemented. There are examples of encryption and decryption functions.

sergej shafarenka
  • 20,071
  • 7
  • 67
  • 86
-1

Replace the below line:

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

with below line:

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding","BC");
Tom
  • 16,842
  • 17
  • 45
  • 54
imnitesh
  • 83
  • 9