title: Java encrypting and decryption of image files using AES and a hashed password as key
body: <p>I've looked at several posts on this topic, such as <a href="https://stackoverflow.com/questions/26058823/correct-way-of-encrypting-and-decrypting-an-image-using-aes">this</a> post to actually encrypt/decrypt, and <a href="https://stackoverflow.com/questions/9536827/generate-key-from-string">this</a> 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 <a href="https://www.youtube.com/watch?v=PCpKGvfN3bk&amp;t=1s&amp;ab_channel=ThamizharasaN" rel="nofollow noreferrer">this YouTube video</a> 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.</p>
  
  <p>This is my function generating a key based on a SHA256 hash passed as <code>k</code>:</p>
  
  <pre><code> 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");
   }
  </code></pre>
  
  <p>These are the functions I'm using to encrypt and decrypt:</p>
  
  <pre><code>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");
      }
  </code></pre>
  
  <p>The function I'm using to write the contents back into the file:</p>
  
  <pre><code>   public static void saveFile(byte[] bytes) throws IOException {
          FileOutputStream fos = new FileOutputStream("filepath/test.jpg");
          fos.write(bytes);
          fos.close();
      }
  </code></pre>
  
  <p>And finally, the function that gets the byte representation of the image file, passed into the encrypt/decrypt functions as "content":</p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>And the main function calling these functions:</p>
  
  <pre><code>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);
  
   }
  </code></pre>
  
  <p>No errors or exceptions are thrown, and the posts and video I referenced while writing this code seems to be working fine.</p>
  
  <p>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.</p>