title: Decrypt method returning null
body: <p>I have this cryptography class.</p>
  
  <pre><code>import java.security.*;
  import javax.crypto.*;
  import javax.crypto.spec.*;
  public class Crypto
  {
      public Crypto(){
  
      }
  
      public static void main(String args[]){
          Crypto crypto = new Crypto();
          byte encrypted[] = crypto.encrypt("test encryption");
          System.out.println(encrypted);
          String decrypted = crypto.decrypt(encrypted);
          System.out.println(decrypted);
  
      }
  
      public byte[] encrypt(String input){
          try{
              Crypto crypto = new Crypto();
              SecretKeySpec key = crypto.hashPhrase();
              Cipher aes = Cipher.getInstance("AES/ECB/PKCS5Padding");
              aes.init(Cipher.ENCRYPT_MODE, key);
              return aes.doFinal(input.getBytes());
          }
          catch(Exception e){
              return null;
          }
      }
  
      public SecretKeySpec hashPhrase(){
          try{
              String code = "some code";
              MessageDigest digest = MessageDigest.getInstance("SHA");
              digest.update(code.getBytes());
              return new SecretKeySpec(digest.digest(), 0, 16, "AES");
          }
          catch(Exception e){
              return null;
          }
      }
  
      public String decrypt(byte[] input){
          try{
              Crypto crypto = new Crypto();
              SecretKeySpec key = crypto.hashPhrase();
              Cipher aes = Cipher.getInstance("AES/ECB/PKCS5Padding");
              aes.init(Cipher.DECRYPT_MODE, key);
              return new String(aes.doFinal(input));
          }
          catch(Exception e){
              return null;
          }
      }
  }
  </code></pre>
  
  <p>When I run main in this class, it works fine. I see an encrypted value, and then after the decrypt method is called I see the original input - "test encryption."</p>
  
  <p>However, when I try to actually use the decrypt method, I run into issues. I've shortened this class a bit to only show the relevant parts.</p>
  
  <pre><code>public void read() throws java.io.IOException{
      Crypto crypto = new Crypto();
      byte[] input;
      BufferedReader in = new BufferedReader(new FileReader("C:\\Budget\\data.txt")); 
      while(in.ready()) {
          input = in.readLine().getBytes();
          BudgetInterface.list.add(crypto.decrypt(input)); //ArrayList of Objects
          System.out.println(crypto.decrypt(input));
          //BudgetInterface.list.add(in.readLine()); - artifact from version without cryptographic capability
      }
      in.close();
  }
  </code></pre>
  
  <p>BudgetInterface.list is an ArrayList of objects, as mentioned, and I'm trying to add the decrypted version of the input to the array, but crypto.decrypt(input) is returning for each line in the file. If I remove the cryptography element, it's able to read in lines from the file with no problem. If I don't try to decrypt, it also reads in just fine. Why does the decrypt method return null in this method, but not in the main method of the Crypto class?</p>
  
  <p>EDIT: The error I'm getting after getting the stack trace is <code>javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher</code>
  I understand what this is saying, but how come this only happens when I get something encrypted from a file? The byte array that I'm reading from the file and the one I'm getting from the main method in Crypto are the same length.</p>