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();
}
}
Edit The stack traces:
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)