加密异常:给定最终块未正确填充

时间:2017-11-08 20:16:42

标签: java encryption bouncycastle

我正在尝试用我自己的密钥解密以下字符串:

“zW4%3D1p1%2AjR9E”

private static void init(String password) throws Exception {
    PBEKeySpec PBEKeySpecification = new PBEKeySpec(password.toCharArray());
    keyDES = SecretKeyFactory.getInstance(encrypted_algorithm).generateSecret(PBEKeySpecification);
    myCipher = Cipher.getInstance(encrypted_algorithm);
    algorithmSpecification = new PBEParameterSpec(salt, iterationCounter);
}

public static void main(String[] args) throws Exception {
    String input = "zW4%3D1p1%2AjR9E";
    String infoDesencriptada = null;
    try {
        init("abc123ab");
        myCipher.init(2, keyDES, algorithmSpecification);
        BASE64Decoder base64Enc = new BASE64Decoder();
        byte[] arrayBase64Enc = base64Enc.decodeBuffer(input);
        byte[] decryptedBytes = myCipher.doFinal(arrayBase64Enc);
        infoDesencriptada = new String(decryptedBytes, "UTF8");
    } catch (Exception var5) {
        System.out.println("Some exception:" + var5.getMessage());
        var5.printStackTrace();
    }
}

我收到此错误: javax.crypto.BadPaddingException:给定最终块未正确填充

任何帮助?

1 个答案:

答案 0 :(得分:1)

在问题文本中,数据为"zW4%3D1p1%2AjR9E",但在代码中为"zW4%3D1p1%2AjR9E7",请注意额外的结尾7

输入必须是块大小的倍数,对于DES是8字节,因此代码中似乎有一个简单的输入错误。