title: How can I read an encrypted text from a file and decrypt it?
body: <p>I have some account info that is being encrypted and written to a file like this:</p>
  
  <pre><code>//imports here
  public class Main 
  
      public static void main(String[] args) {
  
  
          try {
              String text = "this text will be encrypted";
  
              String key = "Bar12345Bar12345";
  
              //Create key and cipher
              Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
              Cipher cipher = Cipher.getInstance("AES");
  
              //encrypt text
              cipher.init(Cipher.ENCRYPT_MODE, aesKey);
              byte[] encrypted = cipher.doFinal(text.getBytes());
              write(new String(encrypted));
  
          } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
              e.printStackTrace();
          }
  
      }
  
      public static void write(String message) {
          BufferedWriter bw = null;
          FileWriter fw = null;
  
          try {
  
              String data = message;
  
              File file = new File(FILENAME);
              if (!file.exists()) {
                  file.createNewFile();
              }
  
  
              fw = new FileWriter(file.getAbsoluteFile(), true);
              bw = new BufferedWriter(fw);
  
              bw.write(data);
  
          } catch (IOException e) {
  
              e.printStackTrace();
  
          } finally {
  
              try {
  
                  if (bw != null)
                      bw.close();
  
                  if (fw != null)
                      fw.close();
  
              } catch (IOException ex) {
  
                  ex.printStackTrace();
  
              }
          }
      }
  
  }
  </code></pre>
  
  <p>So the contents of the file is one string without any breaks in between. If I wanted to decrypt the string, I would do this:</p>
  
  <pre><code>String key = "Bar12345Bar12345";
  Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
  Cipher cipher = Cipher.getInstance("AES");
  byte[] encrypted = text.getBytes(); 
  cipher.init(Cipher.DECRYPT_MODE, aesKey);
  String decrypted = new String(cipher.doFinal(encrypted));
  System.err.println(decrypted);
  </code></pre>
  
  <p>This works fine as long as <code>byte[] encrypted</code> is the same as used as in the encrypting process, but when I try to read the encrypted text from the file using a FileReader and a BufferedReader and change it into a byte using <code>lines.getByte()</code> it throws the exception </p>
  
  <pre><code>javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
  </code></pre>