So I am working on a personal project for myself, and I am trying to encrypt files on my phone. These files can be anything i.e Documents, photos, etc. Right now I am trying to get this working properly. When ever I run the encryption it seems to work properly and encrypt the files. When I run the decrypt, sometimes it works and other times it doesn't. When it fails I generally get a "Error while finalizing cipher, pad block corrupted" error. I'm also not using different test files, so it isn't like some files work and others don't. It's the same two files I try each time.
public static void encryptfile(String path,String Pass) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
FileInputStream fis = new FileInputStream(path);
FileOutputStream fos = new FileOutputStream(path.concat(".crypt"));
byte[] key = (salt + Pass).getBytes("UTF-8");
MessageDigest sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key,16);
SecretKeySpec sks = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, sks);
CipherOutputStream cos = new CipherOutputStream(fos, cipher);
int b;
byte[] d = new byte[8];
while((b = fis.read(d)) != -1) {
cos.write(d, 0, b);
}
cos.flush();
cos.close();
fis.close();
}
public static void decrypt(String path,String Pass) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
FileInputStream fis = new FileInputStream(path);
FileOutputStream fos = new FileOutputStream(path.replace(".crypt",""));
byte[] key = (salt + Pass).getBytes("UTF-8");
MessageDigest sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key,16);
SecretKeySpec sks = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, sks);
CipherInputStream cis = new CipherInputStream(fis, cipher);
int b;
byte[] d = new byte[8];
while((b = cis.read(d)) != -1) {
fos.write(d, 0, b);
}
fos.flush();
fos.close();
cis.close();
}
Currently the Salt and Password are static and do not change for testing purposes. Still get errors about half the time.
Does anyone have any ideas on why this happens? I have been searching around and I have found a couple things to try, none of which worked. I have looked through some of the following questions for solutions:
Android decryption: Error while finalizing cipher
Encryption error on Android 4.2
Decrypting error : "no iv set when one expected"
How to handle "last block incomplete in decryption"
Encryption and decryption of image file
Tips on encryption/decryption of images in java using AES
Any help is greatly appreciated! I think I am just missing something simple...
Update!
People were right when it was the salt. When I removed the salt, the problem was solved... Did a little more digging, turns out salt+Pass was the issues, but because the salt was a byte[] and Pass was a string. I changed salt to String and then used salt.concat(Pass) and the problem was solved!