无法使用Cipher类解密文件 [英] Unable to decrypt a file using Cipher class

查看:155
本文介绍了无法使用Cipher类解密文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wanted to encrypt a file based on a password, so I used the following code:

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 ...
}

它工作正常,加密文件已创建,但当我尝试解密加密文件时,我在最终确定密码时遇到错误。解密方法是:

It worked fine and the encrypted file was created, but when I tried to decrypt the encrypted file, I got Error while finalizing cipher. The decryption method is:

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)


推荐答案

尝试

while ((len = bis.read(buff)) > 0) {
if(input.available() == 0){
  // use descryped file here
  // you can use callback methods
   }
    output.write(buff, 0, len);
}

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

here, inputstream keeps reducing its availability, but it tries after 0 also. Because you did not use cipher.doFinal(fileData.getBytes()).

这篇关于无法使用Cipher类解密文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆