使用Java中的AES-256 / CBC解密OpenSSL命令

时间:2017-09-26 12:44:08

标签: java encryption openssl aes

我使用下面的OpenSSL命令加密密码字符串 -

  

回声" HELLO" | openssl enc -aes-256-cbc -e -a -pass pass:C:\ aes-keystore.jck -iv 629E2E1500B6BA687A385D410D5B08E3> C:\ encrypt.dat

加密输出为 - U2FsdGVkX1 / DhOl5VPwVmcXZGyT0CpXIjO0uUAuBW9Q =

然后使用下面的OpenSSL命令解密加密的字符串 -

  

openssl enc -aes-256-cbc -d -a -pass pass:C:\ aes-keystore.jck -iv 629E2E1500B6BA687A385D410D5B08E3 -in C:\ decrypt.dat

解密输出为-HELLO

我必须使用Java代码解密使用OpenSSL命令生成的文本。

我使用下面的java代码来解密代码 -

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);

}

我低于错误 -

  

java.lang.IllegalArgumentException:解密失败   :: javax.crypto.BadPaddingException:给定最终块不正确   填充

请帮助解决此问题。提前谢谢。

0 个答案:

没有答案