title: AES encryption, got extra trash characters in decrypted file
body: <p>Im making a debug loggin function in an android app.
  I have a simple class which is <strong>logging to .txt file using 128 bit AES encryption.</strong></p>
  <p>After the logging is done, <strong>i decrypt the logged file with a simple JAVA program.</strong></p>
  <p>The problem is when i decrypt the encrypted log <strong>i got some weird content in it,</strong> i also got the encrypted content, but there are some extra characters, see below.</p>
  <p><strong>Android app logging part:</strong></p>
  <pre><code>public class FileLogger {
  
  //file and folder name
  public static String LOG_FILE_NAME = &quot;my_log.txt&quot;;
  public static String LOG_FOLDER_NAME = &quot;my_log_folder&quot;;
  
  static SimpleDateFormat formatter = new SimpleDateFormat(&quot;yyyy-MM-dd_HH-mm-ss_SSS&quot;);
  
  //My secret key, 16 bytes = 128 bit
  static byte[] key = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6};
  
  //Appends to a log file, using encryption
  public static void appendToLog(Context context, Object msg) {
  
      String msgStr;
      String timestamp = &quot;t:&quot; + formatter.format(new java.util.Date());
  
      msgStr = msg + &quot;|&quot; + timestamp + &quot;\n&quot;;
  
      File sdcard = Environment.getExternalStorageDirectory();
      File dir = new File(sdcard.getAbsolutePath() + &quot;/&quot; + LOG_FOLDER_NAME);
      if (!dir.exists()) {
          dir.mkdir();
      }
  
      File encryptedFile = new File(dir, LOG_FILE_NAME);
  
      try {
          
          //Encryption using my key above defined
          Key secretKey = new SecretKeySpec(key, &quot;AES&quot;);
          Cipher cipher = Cipher.getInstance(&quot;AES&quot;);
          cipher.init(Cipher.ENCRYPT_MODE, secretKey);
  
          byte[] outputBytes = cipher.doFinal(msgStr.getBytes());
  
          //Writing to the file using append mode
          FileOutputStream outputStream = new FileOutputStream(encryptedFile, true);
          outputStream.write(outputBytes);
          outputStream.close();
          
          
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      } catch (NoSuchAlgorithmException e) {
          e.printStackTrace();
      } catch (NoSuchPaddingException e) {
          e.printStackTrace();
      } catch (IllegalBlockSizeException e) {
          e.printStackTrace();
      } catch (BadPaddingException e) {
          e.printStackTrace();
      } catch (InvalidKeyException e) {
          e.printStackTrace();
      }
  
  }
  
  }
  </code></pre>
  <p><strong>And this is the decrypter JAVA program:</strong></p>
  <pre><code>public class Main {
  
      
  
  //output file name after decryption
  private static String decryptedFileName;
  //input encrypted file
  private static String fileSource;
  //a prefix tag for output file name
  private static String outputFilePrefix = &quot;decrypted_&quot;;
  //My key for decryption, its the same as in the encrypter program.
  static byte[] key = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 };
  
  //Decrypting function
  public static void decrypt(byte[] key, File inputFile, File outputFile) throws Exception {
      try {
  
          Key secretKey = new SecretKeySpec(key, &quot;AES&quot;);
          Cipher cipher = Cipher.getInstance(&quot;AES&quot;);
          cipher.init(Cipher.DECRYPT_MODE, secretKey);
  
          FileInputStream inputStream = new FileInputStream(inputFile);
          byte[] inputBytes = new byte[(int) inputFile.length()];
          inputStream.read(inputBytes);
  
          byte[] outputBytes = cipher.doFinal(inputBytes);
  
          FileOutputStream outputStream = new FileOutputStream(outputFile, true);
          outputStream.write(outputBytes);
  
          inputStream.close();
          outputStream.close();
  
      } catch (Exception ex) {
          ex.printStackTrace();
      }
  }
  
  //first argument is the intput file source
  public static void main(String[] args) {
  
      if (args.length != 1) {
          System.out.println(&quot;Add log file name as a parameter.&quot;);
  
      } else {
          fileSource = args[0];
  
          try {
              File sourceFile = new File(fileSource);
              if (sourceFile.exists()) {
                  
                  //Decrption
                  decryptedFileName = outputFilePrefix + sourceFile.getName();
                  File decryptedFile = new File(decryptedFileName);
                  decrypt(key, sourceFile, decryptedFile);
              } else {
                  System.out.println(&quot;Log file not found: &quot; + fileSource);
              }
  
          } catch (Exception e) {
              e.printStackTrace();
          }
  
          System.out.println(&quot;Decryption done, output file: &quot; + decryptedFileName);
      }
  
  }
  
  }
  </code></pre>
  <p><strong>Output decrypted log (Opened with notepad++):</strong></p>
  <p><img src="https://i.stack.imgur.com/wIDS5.png" alt="enter image description here" /></p>
  <p>T<strong>here is the valid content, but you also can see the extra thrash characters.</strong> If I open with the default windows text editor i also got thrash charaters, but different ones.</p>
  <p>This is my first try with encrypt -decrypt, <strong>what m i doing wrong?
  Any ideas?</strong></p>