AES/CBC/PKCS5Padding Java 加密错误 - javax.crypto.BadPaddingException:给定的最终块未正确填充

发布于 2025-01-07 08:17:06 字数 670 浏览 0 评论 0原文

我正在尝试使用 AES/CBC/PKCS5Padding 对字符串进行加密解密 我收到此异常:javax.crypto.BadPaddingException:给定的最终块未正确填充

我尝试加密的字符串:ftp.clarapoint.com

这是我的加密代码:

cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");        
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] data = cipher.doFinal(stringDec.getBytes());
byte[] iv = cipher.getIV();

我正在传输以下解密方法:< strong>aesKey、数据和iv

解密代码:

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
AlgorithmParameters.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, aesKey, new IvParameterSpec(iv));
byte[] decrypted = cipher.doFinal(data);

谢谢!

I'm trying to do encryption-decryption of a String using AES/CBC/PKCS5Padding
I'm getting this Exception: javax.crypto.BadPaddingException: Given final block not properly padded

the string i'm trying to encrypt: ftp.clarapoint.com

Here is my encryption code:

cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");        
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] data = cipher.doFinal(stringDec.getBytes());
byte[] iv = cipher.getIV();

I'm transfering the decryption method the following: aesKey, data and iv

the decryption code:

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
AlgorithmParameters.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, aesKey, new IvParameterSpec(iv));
byte[] decrypted = cipher.doFinal(data);

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌︼了一个春 2025-01-14 08:17:06

您没有正确传输密钥或密文,因为此代码确实运行:

private static void testCode() {
    try {
        String stringDec = "Hi there";
        SecretKey aesKey = new SecretKeySpec(new byte[16], "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, aesKey);

        // no encoding given, don't use getBytes() without a Charset.forName("UTF-8")
        byte[] data = cipher.doFinal(stringDec.getBytes());
        byte[] iv = cipher.getIV();

        // doesn't do anything
        AlgorithmParameters.getInstance("AES");

        cipher.init(Cipher.DECRYPT_MODE, aesKey, new IvParameterSpec(iv));
        byte[] decrypted = cipher.doFinal(data);
        System.out.println(new String(decrypted));
    } catch (GeneralSecurityException e) {
        throw new IllegalStateException(e);
    }
}

You are not transfering either the key or the cipher text correctly, as this code does run:

private static void testCode() {
    try {
        String stringDec = "Hi there";
        SecretKey aesKey = new SecretKeySpec(new byte[16], "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, aesKey);

        // no encoding given, don't use getBytes() without a Charset.forName("UTF-8")
        byte[] data = cipher.doFinal(stringDec.getBytes());
        byte[] iv = cipher.getIV();

        // doesn't do anything
        AlgorithmParameters.getInstance("AES");

        cipher.init(Cipher.DECRYPT_MODE, aesKey, new IvParameterSpec(iv));
        byte[] decrypted = cipher.doFinal(data);
        System.out.println(new String(decrypted));
    } catch (GeneralSecurityException e) {
        throw new IllegalStateException(e);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文