0

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.

Community
  • 1
  • 1
CS2016
  • 331
  • 1
  • 3
  • 15
  • 3
    Don't, ever, use available(). It doesn't do what you think it does. Use https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#readAllBytes-java.nio.file.Path- to read a file. – JB Nizet Apr 02 '17 at 17:28
  • You don't show any of the code that calls these methods, so it's impossible to determine why you are getting the results you claim to be getting. – President James K. Polk Apr 02 '17 at 19:15
  • I added the main function calling them. – CS2016 Apr 02 '17 at 19:34
  • Despite your buggy `getFile()` method your code works for me. – President James K. Polk Apr 02 '17 at 20:50
  • @JBNizet Although correct, I can remember that `available` *generally* returns the right value for files. Maybe not for large media files, I'm not sure. So although good advice, there maybe something else wrong as well. – Maarten Bodewes Apr 02 '17 at 21:23

1 Answers1

0

After more trials, I realized the test file I was trying to run the code on was in a cloud drive. I copied the file to a local directory, reran the code and it worked perfectly.

CS2016
  • 331
  • 1
  • 3
  • 15