Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.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
Java 无法使用密码类解密文件_Java_Android_Encryption - Fatal编程技术网

Java 无法使用密码类解密文件

Java 无法使用密码类解密文件,java,android,encryption,Java,Android,Encryption,我想根据密码对文件进行加密,因此使用了以下代码: private void encrypt(String password) { SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); KeySpec ks = new PBEKeySpec( password.toCharArray(), "salt".getBytes(),

我想根据密码对文件进行加密,因此使用了以下代码:

private void encrypt(String password) {
    SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    KeySpec ks = new PBEKeySpec(
            password.toCharArray(),
            "salt".getBytes(),
            1024,
            256
    );
    SecretKey s = f.generateSecret(ks);
    java.security.Key key = new SecretKeySpec(s.getEncoded(), "AES/CBC/PKCS5Padding");

    InputStream input = new FileInputStream("PATH_TO_IMAGE");   
    BufferedInputStream bis = new BufferedInputStream(input);

    FileOutputStream fos = new FileOutputStream("PATH_TO_ENCRYPTED_FILE");
    BufferedOutputStream bos = new BufferedOutputStream(fos);

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

    byte[] buff = new byte[32 * 1024];

    CipherOutputStream output = new CipherOutputStream(bos, cipher);
    int len;
    while ((len = bis.read(buff)) > 0) {
        output.write(buff, 0, len);
    }
    output.flush();

    // closing streams ...
}
它工作正常,并且创建了加密文件,但当我尝试解密加密文件时,在完成密码时出现了
错误。解密方法为:

private void decrypt(String password) {
    SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    KeySpec ks = new PBEKeySpec(
            password.toCharArray(),
            "salt".getBytes(),
            1024,
            256
    );
    SecretKey s = f.generateSecret(ks);
    java.security.Key key = new SecretKeySpec(s.getEncoded(), "AES/CBC/PKCS5Padding");

    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, key);

    InputStream inputStream = new FileInputStream("PATH_TO_ENCRYPTED_FILE");
    CipherInputStream input = new CipherInputStream(inputStream, cipher);
    byte[] data = inputStreamToByteArray(input);

    bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

    // closing streams ...
}

public static byte[] inputStreamToByteArray(CipherInputStream inputStream) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len;
    while ((len = inputStream.read(buffer)) > -1) {
        baos.write(buffer, 0, len);
    }
    baos.flush();

    try {
        return baos.toByteArray();
    } finally {
        baos.close();
    }
}
编辑堆栈跟踪:

05-27 19:51:02.226    2683-2719/? E/MYAPP﹕ exception
    java.io.IOException: Error while finalizing cipher
            at javax.crypto.CipherInputStream.fillBuffer(CipherInputStream.java:104)
            at javax.crypto.CipherInputStream.read(CipherInputStream.java:155)
            at java.io.InputStream.read(InputStream.java:162)
            at github.yaa110.gallery.PrivatePlus.inputStreamToByteArray(PrivatePlus.java:163)
            at github.yaa110.gallery.thread.BitmapLoader.run(BitmapLoader.java:55)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
     Caused by: javax.crypto.BadPaddingException: EVP_CipherFinal_ex
            at com.android.org.conscrypt.NativeCrypto.EVP_CipherFinal_ex(Native Method)
            at com.android.org.conscrypt.OpenSSLCipher.doFinalInternal(OpenSSLCipher.java:430)
            at com.android.org.conscrypt.OpenSSLCipher.engineDoFinal(OpenSSLCipher.java:490)
            at javax.crypto.Cipher.doFinal(Cipher.java:1314)
            at javax.crypto.CipherInputStream.fillBuffer(CipherInputStream.java:102)
            at javax.crypto.CipherInputStream.read(CipherInputStream.java:155)
            at java.io.InputStream.read(InputStream.java:162)
            at github.yaa110.gallery.PrivatePlus.inputStreamToByteArray(PrivatePlus.java:163)
            at github.yaa110.gallery.thread.BitmapLoader.run(BitmapLoader.java:55)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
试一试


在这里,inputstream不断降低其可用性,但它也会在0之后尝试。因为您没有使用cipher.doFinal(fileData.getBytes())

你能给出完整的堆栈跟踪吗?@JPMoresmau添加了更多的堆栈跟踪。输入(明文)和输出(加密)文件有多少字节?@Robert输入是具有不同字节的图像文件(加密文件的字节数从2到24 KB)@yaa110然后选择一个示例,告诉我们它们的大小,或者尝试加密/解密文本文件,看看解密是否有效。。。
while ((len = bis.read(buff)) > 0) {
if(input.available() == 0){
  // use descryped file here
  // you can use callback methods
   }
    output.write(buff, 0, len);
}