javax.crypto.BadPaddingException:填充块损坏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21988552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:52:23  来源:igfitidea点击:

javax.crypto.BadPaddingException: pad block corrupted

javaandroid

提问by Ran

I'm trying to encrypt something, and decrypt it. I'm failing on the decryption - I get the exception above. I tried changing ctLength and ptLength, but to no avail. What am I doing wrong?
I'm trying to encrypt: 0 0 0 0 0 0 0 0

我试图加密一些东西,然后解密它。我解密失败 - 我得到了上面的例外。我尝试更改 ctLength 和 ptLength,但无济于事。我究竟做错了什么?
我正在尝试加密:0 0 0 0 0 0 0 0

private Cipher encrypt(byte[] input)
{
    try
    {
        SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");

        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");

        // encryption pass
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
        int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
        ctLength += cipher.doFinal(cipherText, ctLength);
        FileOutputStream fs = new FileOutputStream(savedScoresFileName);
        fs.write(cipherText);

        return cipher;
    }
    catch (Exception e)
    {
        Log.e("encrtypt", "Exception", e);
    }

    return null;
}

private String decrypt()
{
    try
    {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");

        SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
        byte[] cipherText = new byte[32];

        FileInputStream fl = new FileInputStream(savedScoresFileName);
        fl.read(cipherText);

        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] plainText = new byte[cipher.getOutputSize(32)];
        int ptLength = cipher.update(cipherText, 0, 32, plainText, 0);
        ptLength += cipher.doFinal(plainText, ptLength);

        return new String(plainText).substring(0, ptLength);
    }
    catch (Exception e)
    {
        Log.e("decrypt", "Exception", e);
    }

    return null;
}

This code was copied from this, which worked.

此代码是从this复制的,它有效。

采纳答案by Duncan Jones

Your code has a number of issues, but your problem is caused by your file reading code and your strange method of performing the encryption and decryption.

您的代码有很多问题,但您的问题是由您的文件读取代码和执行加密和解密的奇怪方法引起的。

Don't use the update()method, just use doFinal()and correct your file writing/reading code. E.g. your decryption method should look something like:

不要使用该update()方法,只需使用doFinal()并更正您的文件写入/读取代码。例如,您的解密方法应该类似于:

try {
  Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");

  SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");

  // Here you need to accurately and correctly read your file into a byte
  // array. Either Google for a decent solution (there are many out there)
  // or use an existing implementation, such as Apache Commons commons-io.
  // Your existing effort is buggy and doesn't close its resources.      
  byte[] cipherText = FileUtils.readFileToByteArray(new File(savedScoresFileName));


  cipher.init(Cipher.DECRYPT_MODE, key);

  // Just one call to doFinal
  byte[] plainText = cipher.doFinal(cipherText);

  // Note: don't do this. If you create a string from a byte array,
  // PLEASE pass a charset otherwise your result is platform dependent.
  return new String(plainText);
} catch (Exception e) {
  e.printStackTrace();
}