2828import javax .crypto .spec .PBEKeySpec ;
2929import javax .crypto .spec .SecretKeySpec ;
3030
31-
3231/**
3332 *
3433 * @author Erik Costlow
@@ -146,7 +145,7 @@ public static void main(String[] args) throws Exception {
146145 * Encrypts a plain text input file by outputing an encrypted version. It does this
147146 * generating a secret key from a passowrd and an initialisation vector which are
148147 * used as the specifications during the file encryption process. A message
149- * authentication code is also computed and initialised with the vector and plaintext
148+ * authentication code is also computed and initialised with the metadata and plaintext
150149 * values, hence they can be checked for tampering during decryption.
151150 *
152151 * @param password char[] The password specified by the user
@@ -200,6 +199,9 @@ public static void encrypt(char[] password, String inputPath, String outputPath)
200199
201200 Cipher cipher = createCipher (key , initVector , 1 );
202201
202+ // Keys no longer needed clearing keys
203+ Arrays .fill (key , (byte ) 0 ); Arrays .fill (macKey , (byte ) 0 );
204+
203205 // Write plaintext into ciphertext
204206 if (writeEncryptedFile (plaintextFile , encryptedFile , cipher , salt , macSalt , mac )) {
205207 LOG .info ("Encryption finished, saved at " + encryptedFile );
@@ -289,10 +291,10 @@ public static void decrypt(char[] password, String inputPath, String outputPath)
289291 * Reads an encrypted file by wrapping an InputStream with a CipherInputStream
290292 * The encrypted files gets decrypted and written out to the output file.
291293 * For a successful decryption the Cipher needs to be initialized in DECRYPT mode
292- * with the correct key and vector specifications. The IV, salts and mac is read
293- * from the encrypted file as it was saved as metadata during the encryption process.
294+ * with the correct key and vector specifications. The metadata embeded is read
295+ * from the encrypted file which was saved/written during the encryption process.
294296 * Decryption will also fail if the computed authentication code doesn't match with
295- * the given authentication code.
297+ * the given message authentication code.
296298 *
297299 * @param inputPath Path The input file path (encrypted file)
298300 * @param outputPath Path The output file path (decrypted file)
@@ -356,6 +358,9 @@ private static boolean writeDecryptedFile(Path inputPath, Path outputPath, char[
356358 displayInformation (getPair ("Secret Key" , key ), getPair ("Init Vector" , initVector ), getPair ("Salt" , salt ),
357359 getPair ("Mac Key" , macKey ), getPair ("Mac salt" , macSalt ), getPair ("Computed Mac" , computedMac ),
358360 getPair ("Given Mac" , givenMac ));
361+
362+ // Keys no longer needed clearing keys
363+ Arrays .fill (key , (byte ) 0 ); Arrays .fill (macKey , (byte ) 0 );
359364
360365 LOG .info ("Authentication passed, file integrity maintained" );
361366
@@ -370,6 +375,7 @@ private static boolean writeDecryptedFile(Path inputPath, Path outputPath, char[
370375 * Allows the user to query metadata for a given file path. The file path
371376 * specified must point to an encrypted file with a .enc extension The metadata
372377 * for the file must also follow a specific format as shown below.
378+ *
373379 * Metadata format:
374380 * int BLOCKSIZE
375381 * int KEY LENGTH (in bytes)
@@ -380,7 +386,7 @@ private static boolean writeDecryptedFile(Path inputPath, Path outputPath, char[
380386 * byte[] MacSalt
381387 * byte[] Computed Mac
382388 *
383- * @param String filepath The file being requested to be display the metadata
389+ * @param filepath String The file being requested to be display the metadata
384390 */
385391 private static void info (String filepath ) {
386392 if (!filepath .contains (".enc" )) { throw new IllegalArgumentException ("Invalid file requested must be an encrypted file e.g. encrypted.enc" ); }
0 commit comments