I am encrypting the password string using below OpenSSL command -
echo "HELLO" |openssl enc -aes-256-cbc -e -a -pass pass:C:\aes-keystore.jck -iv 629E2E1500B6BA687A385D410D5B08E3 > C:\encrypt.dat
Encrypted output is - U2FsdGVkX1/DhOl5VPwVmcXZGyT0CpXIjO0uUAuBW9Q=
And then decrypt the encrypted string using below OpenSSL command-
openssl enc -aes-256-cbc -d -a -pass pass:C:\aes-keystore.jck -iv 629E2E1500B6BA687A385D410D5B08E3 -in C:\decrypt.dat
Decrypted output is -HELLO
I have to decrypt the text generated using OpenSSL command using Java code.
I am using below java code to decrypt the code -
public static String decryptKey(String dataForDecryption) {
Cipher cipher;
byte[] decryptedData = null;
final byte[] initVector = javax.xml.bind.DatatypeConverter.parseHexBinary("629E2E1500B6BA687A385D410D5B08E3");
try {
if (dataForDecryption != null) {
Key key = getKeyFromKeyStore(); //This method will return the key from keystore.
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKey secKey = new SecretKeySpec(key.getEncoded(), ENCRYPTION_TYPE);
cipher.init(Cipher.DECRYPT_MODE, secKey, new IvParameterSpec(initVector, 0, cipher.getBlockSize()));
decryptedData = cipher.doFinal(Base64.decodeBase64(dataForDecryption.getBytes()));
}
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException
| BadPaddingException e) {
LOG.error("Error getting in decrypting the ASE Key:");
throw new IllegalArgumentException("Decryption failed ::" + e);
} catch (InvalidAlgorithmParameterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new String(decryptedData);
}
I am getting below Error -
java.lang.IllegalArgumentException: Decryption failed ::javax.crypto.BadPaddingException: Given final block not properly padded
Please help to fix this issue. Thanks in advance.