Im doing the debug loggin function in an android app. I have a simple class that is written to the .txt file using 128-bit AES-encryption.
After logging is complete, I decrypt the registered file using a simple JAVA program.
The problem is when I decrypt the encrypted log , I received some strange content in it , I also received the encrypted content, but there are additional characters, see below.
Android Application Registration Part:
public class FileLogger { //file and folder name public static String LOG_FILE_NAME = "my_log.txt"; public static String LOG_FOLDER_NAME = "my_log_folder"; static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss_SSS"); //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 = "t:" + formatter.format(new java.util.Date()); msgStr = msg + "|" + timestamp + "\n"; File sdcard = Environment.getExternalStorageDirectory(); File dir = new File(sdcard.getAbsolutePath() + "/" + 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, "AES"); Cipher cipher = Cipher.getInstance("AES"); 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(); } } }
And this is the JAVA decrypter program:
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 = "decrypted_"; //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, "AES"); Cipher cipher = Cipher.getInstance("AES"); 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("Add log file name as a parameter."); } 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("Log file not found: " + fileSource); } } catch (Exception e) { e.printStackTrace(); } System.out.println("Decryption done, output file: " + decryptedFileName); } } }
Decrypted log output (opened with notepad ++):

Here is the valid content, but you can also see additional thrash characters. If I open the default Windows text editor, I also have trash characters, but different ones.
This is my first attempt with encrypt -decrypt, what am I doing wrong? Any ideas?