@@ -78,6 +78,19 @@ public static void main(String[] args) throws Exception {
7878 charArgs = null ; dec = null ; enc = null ;
7979 }
8080
81+ /**
82+ * Generates a Secret key with a specified password. The password is added with
83+ * a salt and iterated multiple times before being hased to increase entropy.
84+ * The salt and key lenghts need to be specified to then return a secret key
85+ * encoded in a byte array.
86+ *
87+ * @param password char[] The password specified by the user
88+ * @param salt byte[] A randomly gnerated set of bytes
89+ * @param keyLength int The lenght of the final key, in bits e.g. 128, 256 etc.
90+ * @return byte[] An encoded byte array of the secret key
91+ * @throws NoSuchAlgorithmException
92+ * @throws InvalidKeySpecException
93+ */
8194 private static byte [] generateKey (char [] password , byte [] salt , int keyLength ) throws NoSuchAlgorithmException ,
8295 InvalidKeySpecException {
8396 PBEKeySpec passwordKeySpec = new PBEKeySpec (password , salt , ITERATION_COUNT , keyLength );
@@ -93,9 +106,9 @@ private static byte[] generateKey(char[] password, byte[] salt, int keyLength) t
93106 * is also computed with the intialisaton vector and plaintext values, hence these
94107 * values can be checked for tampering during decryption.
95108 *
96- * @param key byte [] The secrect key which will be used to encrypt the file
97- * @param inputPath - A String specifying the Input path of the plaintext file
98- * @param outputPath - A String specifying the Ouput path of the ciphertext file
109+ * @param password char [] The password specified by the user
110+ * @param inputPath String specifying the Input path of the plaintext file
111+ * @param outputPath String specifying the Ouput path of the ciphertext file
99112 * @throws NoSuchAlgorithmException
100113 * @throws NoSuchPaddingException
101114 * @throws InvalidKeyException
@@ -106,9 +119,7 @@ private static byte[] generateKey(char[] password, byte[] salt, int keyLength) t
106119 public static void encrypt (char [] password , String inputPath , String outputPath ) throws NoSuchAlgorithmException ,
107120 NoSuchPaddingException , InvalidKeyException , InvalidAlgorithmParameterException , IOException , InvalidKeySpecException {
108121 //Generate vector and salts
109- final byte [] initVector = new byte [16 ];
110- final byte [] salt = new byte [16 ];
111- final byte [] macSalt = new byte [16 ];
122+ final byte [] initVector = new byte [16 ], salt = new byte [16 ], macSalt = new byte [16 ];
112123
113124 SecureRandom sr = new SecureRandom ();
114125 sr .nextBytes (initVector );
@@ -147,7 +158,10 @@ public static void encrypt(char[] password, String inputPath, String outputPath)
147158 // Display the Base64 encoded versions of Key, Vector and computed mac
148159 System .out .print ("\n <---------------------------------------->\n " );
149160 System .out .println ("Secret Key is: " + Base64 .getEncoder ().encodeToString (key ));
161+ System .out .println ("Key salt is: " + Base64 .getEncoder ().encodeToString (salt ));
150162 System .out .println ("IV is: " + Base64 .getEncoder ().encodeToString (initVector ));
163+ System .out .println ("Mac Key is: " + Base64 .getEncoder ().encodeToString (macKey ));
164+ System .out .println ("Mac salt is: " + Base64 .getEncoder ().encodeToString (macSalt ));
151165 System .out .println ("Computed Mac: " + Base64 .getEncoder ().encodeToString (mac ));
152166 System .out .print ("<---------------------------------------->\n \n " );
153167
@@ -171,6 +185,8 @@ public static void encrypt(char[] password, String inputPath, String outputPath)
171185 * @param outputPath Path The file path of the output file (ciphertext)
172186 * @param cipher Cipher The cipher instance initialized with the appropriate
173187 * specifications in ENCRYPT mode
188+ * @param salt byte[] The salt used to create key from password
189+ * @param macSalt byte[] The salt used to create the macKey from password
174190 * @return boolean True if encryption successful False otherwise
175191 */
176192 private static boolean writeEncryptedFile (Path inputPath , Path outputPath , Cipher cipher , byte [] salt , byte [] macSalt , byte [] mac ) {
@@ -228,9 +244,9 @@ private static byte[] computeMac(Mac hmac, Path filePath) {
228244 * to create the Cipher specifications required for decryption.
229245 * Will overwrite the resultant output file if it already exists.
230246 *
231- * @param key byte [] - The Key used to originally encrypt the input file
232- * @param inputPath String - The input file path (encrypted document)
233- * @param outputPath String - The file path of the resultant decrypted text
247+ * @param password char [] The password specified by the user
248+ * @param inputPath String The input file path (encrypted document)
249+ * @param outputPath String The file path of the resultant decrypted text
234250 * @throws NoSuchAlgorithmException
235251 * @throws NoSuchPaddingException
236252 * @throws InvalidKeyException
@@ -280,10 +296,7 @@ private static boolean writeDecryptedFile(Path inputPath, Path outputPath, char[
280296 try (InputStream encryptedData = Files .newInputStream (inputPath );){
281297
282298 // Read metadata from the input file
283- final byte [] initVector = new byte [16 ];
284- final byte [] salt = new byte [16 ];
285- final byte [] macSalt = new byte [16 ];
286- final byte [] givenMac = new byte [32 ];
299+ final byte [] initVector = new byte [16 ], salt = new byte [16 ], macSalt = new byte [16 ], givenMac = new byte [32 ];
287300
288301 encryptedData .read (initVector );
289302 encryptedData .read (salt );
0 commit comments