I've looked at several posts on this topic, such as this post to actually encrypt/decrypt, and this post on generating a key using a string as a seed to generate a random key. The encryption reduces my filesize from 3mb to 16 bytes and the decryption reduces it further to 0 bytes. I also watched this YouTube video on the topic and my code had the same issues with the file size being reduced to zero in the decryption process, while his worked fine.
This is my function generating a key based on a SHA256 hash passed as k
:
public static Key keyGen(String k) throws Exception {
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
KeySpec spec = new PBEKeySpec(k.toCharArray(), k.getBytes(), 12, 128);
SecretKey tmp = factory.generateSecret(spec);
return new SecretKeySpec(tmp.getEncoded(), "AES");
}
These are the functions I'm using to encrypt and decrypt:
public static void encrypt(Key key, byte[] content) throws Exception {
Cipher cipher;
byte[] encrypted = null;
try {
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
encrypted = cipher.doFinal(content);
} catch (Exception e) {
e.printStackTrace();
}
saveFile(encrypted);
JOptionPane.showMessageDialog(null, "Encryption complete");
}
public static void decrypt(Key key, byte[] textCryp) throws Exception {
Cipher cipher;
byte[] decrypted = null;
try {
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
decrypted = cipher.doFinal(textCryp);
} catch (Exception e) {
e.printStackTrace();
}
saveFile(decrypted);
JOptionPane.showMessageDialog(null, "Decryption complete");
}
The function I'm using to write the contents back into the file:
public static void saveFile(byte[] bytes) throws IOException {
FileOutputStream fos = new FileOutputStream("filepath/test.jpg");
fos.write(bytes);
fos.close();
}
And finally, the function that gets the byte representation of the image file, passed into the encrypt/decrypt functions as "content":
public static byte[] getFile() {
File f = new File("filepath/test.jpg");
InputStream is = null;
try {
is = new FileInputStream(f);
} catch (FileNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
byte[] content = null;
try {
content = new byte[is.available()];
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
is.read(content);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return content;
}
And the main function calling these functions:
public static void main(String[] args) throws Exception {
Key key = keyGen("1234567812345678");
byte[] fileContents = getFile();
//I change to decrypt after I call encrypt
encrypt(key,fileContents);
}
No errors or exceptions are thrown, and the posts and video I referenced while writing this code seems to be working fine.
I would really appreciate an advice on this as this is the final part of a project I've been working on for a long time.