@@ -108,24 +108,28 @@ public static void encrypt(char[] password, String inputPath, String outputPath)
108108 //Generate vector and salts
109109 final byte [] initVector = new byte [16 ];
110110 final byte [] salt = new byte [16 ];
111+ final byte [] macSalt = new byte [16 ];
111112
112113 SecureRandom sr = new SecureRandom ();
113114 sr .nextBytes (initVector );
114115 sr .nextBytes (salt );
116+ sr .nextBytes (macSalt );
115117
118+ // Get Keys from password
116119 final byte [] key = generateKey (password , salt , 128 );
120+ final byte [] macKey = generateKey (password , macSalt , 256 );
117121
118122 // Initialize Vector and Keys
119123 IvParameterSpec iv = new IvParameterSpec (initVector );
120124 SecretKeySpec skeySpec = new SecretKeySpec (key , ALGORITHM );
121- SecretKeySpec macKey = new SecretKeySpec (key , HASH_AlGORITHM );
125+ SecretKeySpec macKeySpec = new SecretKeySpec (macKey , HASH_AlGORITHM );
122126
123127 // Initialize cipher and Mac
124128 Cipher cipher = Cipher .getInstance (CIPHER );
125129 cipher .init (Cipher .ENCRYPT_MODE , skeySpec , iv );
126130
127131 Mac hmac = Mac .getInstance (HASH_AlGORITHM );
128- hmac .init (macKey );
132+ hmac .init (macKeySpec );
129133
130134 File outputFile = new File (outputPath );
131135 // Create the output file if it doesn't exist
@@ -136,6 +140,8 @@ public static void encrypt(char[] password, String inputPath, String outputPath)
136140
137141 // Compute Mac for authentication
138142 hmac .update (initVector );
143+ hmac .update (salt );
144+ hmac .update (macSalt );
139145 final byte [] mac = computeMac (hmac , plaintextFile );
140146
141147 // Display the Base64 encoded versions of Key, Vector and computed mac
@@ -146,7 +152,7 @@ public static void encrypt(char[] password, String inputPath, String outputPath)
146152 System .out .print ("<---------------------------------------->\n \n " );
147153
148154 // Write plaintext into ciphertext
149- if (writeEncryptedFile (plaintextFile , encryptedFile , cipher , salt , mac )) {
155+ if (writeEncryptedFile (plaintextFile , encryptedFile , cipher , salt , macSalt , mac )) {
150156 LOG .info ("Encryption finished, saved at " + encryptedFile );
151157 } else {
152158 LOG .log (Level .WARNING , "Encryption Failed" );
@@ -167,13 +173,14 @@ public static void encrypt(char[] password, String inputPath, String outputPath)
167173 * specifications in ENCRYPT mode
168174 * @return boolean True if encryption successful False otherwise
169175 */
170- private static boolean writeEncryptedFile (Path inputPath , Path outputPath , Cipher cipher , byte [] salt , byte [] mac ) {
176+ private static boolean writeEncryptedFile (Path inputPath , Path outputPath , Cipher cipher , byte [] salt , byte [] macSalt , byte [] mac ) {
171177 try (InputStream fin = Files .newInputStream (inputPath );) {
172178
173179 try (FileOutputStream fout = new FileOutputStream (outputPath .toFile ());) {
174180 // Write Metadata
175181 fout .write (cipher .getIV ());
176182 fout .write (salt );
183+ fout .write (macSalt );
177184 fout .write (mac );
178185
179186 try (CipherOutputStream cipherOut = new CipherOutputStream (fout , cipher );) {
@@ -275,18 +282,21 @@ private static boolean writeDecryptedFile(Path inputPath, Path outputPath, char[
275282 // Read metadata from the input file
276283 final byte [] initVector = new byte [16 ];
277284 final byte [] salt = new byte [16 ];
285+ final byte [] macSalt = new byte [16 ];
278286 final byte [] givenMac = new byte [32 ];
279287
280288 encryptedData .read (initVector );
281289 encryptedData .read (salt );
290+ encryptedData .read (macSalt );
282291 encryptedData .read (givenMac );
283292
284293 final byte [] key = generateKey (password , salt , 128 );
294+ final byte [] macKey = generateKey (password , macSalt , 256 );
285295
286296 // Create key specifications
287297 IvParameterSpec iv = new IvParameterSpec (initVector );
288298 SecretKeySpec skeySpec = new SecretKeySpec (key , ALGORITHM );
289- SecretKeySpec macKey = new SecretKeySpec (key , HASH_AlGORITHM );
299+ SecretKeySpec macKeySpec = new SecretKeySpec (macKey , HASH_AlGORITHM );
290300
291301 // Initialise cipher
292302 Cipher cipher = Cipher .getInstance (CIPHER );
@@ -304,10 +314,12 @@ private static boolean writeDecryptedFile(Path inputPath, Path outputPath, char[
304314
305315 // Check authentication and file integerity
306316 Mac hmac = Mac .getInstance (HASH_AlGORITHM );
307- hmac .init (macKey );
317+ hmac .init (macKeySpec );
308318
309319 hmac .update (initVector );
310- byte [] computedMac = computeMac (hmac , outputPath );
320+ hmac .update (salt );
321+ hmac .update (macSalt );
322+ final byte [] computedMac = computeMac (hmac , outputPath );
311323 if (!Arrays .equals (givenMac , computedMac )) {
312324 throw new SecurityException ("Authentication failed, file may have been tampered with" );
313325 }
0 commit comments