Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javax.crypto.BadPaddingException:填充块已损坏_Java_Android - Fatal编程技术网

javax.crypto.BadPaddingException:填充块已损坏

javax.crypto.BadPaddingException:填充块已损坏,java,android,Java,Android,我想加密一些东西,然后解密。我解密失败-我得到上面的异常。我尝试更改ctLength和ptLength,但没有效果。我做错了什么? 我正在尝试加密:0 private Cipher encrypt(byte[] input) { try { SecretKeySpec key = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS

我想加密一些东西,然后解密。我解密失败-我得到上面的异常。我尝试更改ctLength和ptLength,但没有效果。我做错了什么?
我正在尝试加密: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;
}

此代码是从复制而来的,有效。

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

不要使用
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();
}

谢谢我不知道加密-我复制并粘贴了过去对我有用的代码。。。