Encrypt files with AES on Android
So I'm working on a personal project for myself and I'm trying to encrypt files on my phone. These files can be anything like documents, photos, etc. Now I am trying to get it working. Whenever I run encrypt it seems to work fine and encrypt the file. When I run decrypt, sometimes it works and sometimes it doesn't. When it fails, I usually get the "Error determining password, padding block is corrupted" error. I also didn't use different test files, so it's not like some files work and others don't. I try the same two files every 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, Salt and passwords are static and will not be changed for testing purposes. Still getting it wrong about half the time.
Does anyone know why this is happening? I've been searching and found some things to try, but to no avail. I've pored over some of the following questions for a solution:
Android decryption: Error finalizing password
Decryption error: "Expected no IV set"
How to handle "The last block in decryption is incomplete"
Encryption and decryption of image files
Tips for encrypting/decrypting images in Java using AES
Any help is greatly appreciated! I think I'm just missing something simple...
renew!
People are right that it's salt. When I removed the salt, the problem was solved. . . Did some more digging and it turned out that salt + Pass was the problem, but since salt is a byte[] and Pass is a string. I changed the salt to String, then used salt.concat(Pass), problem solved!
Maybe I'm missing something, but in my case it works fine. Could you try the following class just by changing the fileToBeCrypted, fileToBeDecrypted and fileDecryptedOutput variables?
package test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
public class TestCrypt{
private static final String salt = "t784";
private static final String cryptPassword = "873147cbn9x5'2 79'79314";
private static final String fileToBeCrypted = "c:\\Temp\\sampleFile.conf";
private static final String fileToBeDecrypted = "c:\\Temp\\sampleFile.conf.crypt";
private static final String fileDecryptedOutput = "c:\\Temp\\sampleFile.conf.decrypted";
public static void main(String[] args) throws Exception
{
for (int i=0; i<100; i++)
{
encryptfile(fileToBeCrypted, cryptPassword);
decrypt(fileToBeDecrypted, cryptPassword, fileDecryptedOutput);
System.out.println(i);
}
}
public static void encryptfile(String path,String password) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
FileInputStream fis = new FileInputStream(path);
FileOutputStream fos = new FileOutputStream(path.concat(".crypt"));
byte[] key = (salt + password).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 password, String outPath) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
FileInputStream fis = new FileInputStream(path);
FileOutputStream fos = new FileOutputStream(outPath);
byte[] key = (salt + password).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();
}
}
I can iterate multiple times without error! I am using Oracle JDK 1.8 but running in 1.7 compatibility mode.
Hope this helps you.
goodbye piero