Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android javax.crypto.BadPaddingException

I am implementing encryption/decryption to my files. code is given below. I can't find out the problem. Am I missing anything? I need to implement 128 bit AES Encryption. Is there anything wrong? It end up with error

 "javax.crypto.BadPaddingException: error:1e06b065:Cipher functions:EVP_DecryptFinal_ex:BAD_DECRYPT"

Please help me.

private static byte[] encodeFile(byte[] yourKey, byte[] fileData)
        throws Exception {
    byte[] encrypted = null;
    SecretKeySpec skeySpec = new SecretKeySpec(yourKey, 0, yourKey.length, "AES");
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7PADDING");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    encrypted = cipher.doFinal(fileData);
    return encrypted;
}

private static byte[] generateKey() throws NoSuchAlgorithmException {
    byte[] keyStart = "This is my key".getBytes();
    String id = "dummypass";
    int iterationCount = 1000;
    int saltLength = 32;
    int keyLength = 128;
    SecureRandom random = new SecureRandom();
    byte[] salt = Arrays.copyOf(keyStart,saltLength);
    random.nextBytes(salt);
    KeySpec keySpec = new PBEKeySpec(id.toCharArray(), salt,
            iterationCount, keyLength);
    SecretKeyFactory keyFactory = SecretKeyFactory
            .getInstance("PBKDF2WithHmacSHA1");
    byte[] keyBytes = new byte[0];
    try {
        keyBytes = keyFactory.generateSecret(keySpec).getEncoded();
    } catch (InvalidKeySpecException e) {
        e.printStackTrace();
    }
    SecretKey key = new SecretKeySpec(keyBytes, "AES");
    return key.getEncoded();
}

private static byte[] decodeFile(byte[] yourKey, byte[] encryptedData)
        throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(yourKey, 0, yourKey.length,
            "AES");
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7PADDING");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec);
    byte[] decrypted = cipher.doFinal(encryptedData);
    return decrypted;
}

public static void Encrypt(byte[] bytesToEncrypt, File target) {
    try {
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream(target));
        byte[] key = generateKey();
        byte[] encryptedBytes = encodeFile(key, bytesToEncrypt);
        bos.write(encryptedBytes);
        bos.flush();
        bos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
public static  byte[] DecryptFile(byte[] bytesToDecrypt) {
    byte[] decodedData = new byte[0];
    try {
        byte[] key = generateKey();
        decodedData = decodeFile(key, bytesToDecrypt);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return decodedData;
}
like image 332
Alvin Varghese Avatar asked Nov 14 '25 11:11

Alvin Varghese


1 Answers

Verify that key is the same in both Encrypt and DecryptFile.

Since both call generateKey and generateKey calls SecureRandom nextBytes the keys are going to be different.

You need to save the encryption key for use during decryption.

like image 176
zaph Avatar answered Nov 17 '25 10:11

zaph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!