1

我有一个 Python 应用程序,它创建了一些使用 AES/PKCS7 加密的文件。我必须使用 Java 服务读取这些文件。但是我的代码抛出了一个异常:

“javax.crypto.IllegalBlockSizeException:最后一个块在解密中不完整”

这是我的解密代码:

public String Decrypt(String strText)
{
    try
    {
        // Text to decrypt
        byte[] test = strText.getBytes();

        //bytKey is the same key as Python app
        SecretKeySpec objKey = new SecretKeySpec(bytKey, "AES");
        Cipher objCipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
        objCipher.init(Cipher.DECRYPT_MODE, objKey);

        // Here I got an exception >>
        byte[] bytValue = objCipher.doFinal(test);

        return new String(bytValue);
    }
    catch (Exception exc)
    {
        exc.printStackTrace();
    }

    return "";

}

如果我在进入之前解码加密文本,doFinal我会得到另一个异常:

“javax.crypto.BadPaddingException:垫块损坏”

public String Decrypt(String strText)
{
    try
    {

        BASE64Decoder decoder = new BASE64Decoder();
        byte[] test = decoder.decodeBuffer(strText);

        SecretKeySpec objKey = new SecretKeySpec(bytKey, "AES");
        Cipher objCipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
        objCipher.init(Cipher.DECRYPT_MODE, objKey);

        byte[] bytValue = objCipher.doFinal(test);

        return new String(bytValue);
    }
    catch (Exception exc)
    {
        exc.printStackTrace();
    }

    return "";

}

我不是加密/解密方面的专家,我想这很容易解决。有什么想法可以解决这个问题吗?提前致谢!

4

0 回答 0