无法使用Cipher类解密文件

时间:2015-05-26 19:12:02

标签: java android encryption

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

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

它工作正常,加密文件已创建,但当我尝试解密加密文件时,我得到了Error while finalizing cipher。解密方法是:

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)

1 个答案:

答案 0 :(得分:0)

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())。