@@ -45,7 +45,7 @@ public static void main(String[] args) throws Exception {
45
45
if (args .length < 1 ) { throw new IllegalArgumentException ("Not Enough Argunments specified\n " + validCmdMsg ); }
46
46
47
47
// Convert String arguments to char arrays
48
- char [][] charArgs = Util .getCharArgunments (args );
48
+ char [][] charArgs = Util .getCharArguments (args );
49
49
50
50
// Clear String argunments
51
51
Arrays .fill (args , null );
@@ -56,9 +56,7 @@ public static void main(String[] args) throws Exception {
56
56
return ;
57
57
}
58
58
59
- if (charArgs .length < 4 ) {
60
- throw new IllegalArgumentException ("Not Enough Argunments Provided\n " + validCmdMsg );
61
- }
59
+ if (charArgs .length < 4 ) { throw new IllegalArgumentException ("Not Enough Argunments Provided\n " + validCmdMsg ); }
62
60
63
61
// Options Available
64
62
char [] enc = "enc" .toCharArray ();
@@ -68,7 +66,7 @@ public static void main(String[] args) throws Exception {
68
66
throw new IllegalArgumentException ("Neither enc (encrypt) or dec (decrypt) option specified\n " + validCmdMsg );
69
67
}
70
68
71
- byte [] key ;
69
+ byte [] key = new byte [ 16 ] ;
72
70
try {
73
71
key = Base64 .getDecoder ().decode (Util .convertCharToByte (charArgs [1 ]));
74
72
} catch (IllegalArgumentException e ) {
@@ -113,7 +111,7 @@ public static void encrypt(byte[] key, String inputPath, String outputPath) thro
113
111
NoSuchPaddingException , InvalidKeyException , InvalidAlgorithmParameterException , IOException {
114
112
//Generate Initilisation Vector
115
113
SecureRandom sr = new SecureRandom ();
116
- byte [] initVector = new byte [16 ];
114
+ final byte [] initVector = new byte [16 ];
117
115
sr .nextBytes (initVector ); // 16 bytes IV
118
116
119
117
// Initialize Vector and Keys
@@ -136,7 +134,7 @@ public static void encrypt(byte[] key, String inputPath, String outputPath) thro
136
134
137
135
// Compute Mac for authentication
138
136
hmac .update (initVector );
139
- byte [] mac = computeMac (hmac , plaintextFile );
137
+ final byte [] mac = computeMac (hmac , plaintextFile );
140
138
141
139
// Display the Base64 encoded versions of Vector and computed mac
142
140
System .out .print ("\n <---------------------------------------->\n " );
@@ -206,7 +204,8 @@ private static byte[] computeMac(Mac hmac, Path filePath) {
206
204
hmac .update (bytes , 0 , length );
207
205
}
208
206
} catch (IOException e ) {
209
-
207
+ LOG .log (Level .SEVERE , "IOException caught - Please check filepath specified" );
208
+ System .exit (0 );
210
209
}
211
210
212
211
return hmac .doFinal ();
@@ -275,12 +274,13 @@ public static void decrypt(byte[] key, String inputPath, String outputPath) thro
275
274
* @throws InvalidKeyException
276
275
* @throws InvalidAlgorithmParameterException
277
276
*/
278
- private static boolean writeDecryptedFile (Path inputPath , Path outputPath , byte [] key ) throws NoSuchAlgorithmException , NoSuchPaddingException , InvalidKeyException , InvalidAlgorithmParameterException {
277
+ private static boolean writeDecryptedFile (Path inputPath , Path outputPath , byte [] key ) throws NoSuchAlgorithmException ,
278
+ NoSuchPaddingException , InvalidKeyException , InvalidAlgorithmParameterException {
279
279
try (InputStream encryptedData = Files .newInputStream (inputPath );){
280
280
281
281
// Read metadata from the input file
282
- byte [] initVector = new byte [16 ];
283
- byte [] givenMac = new byte [32 ];
282
+ final byte [] initVector = new byte [16 ];
283
+ final byte [] givenMac = new byte [32 ];
284
284
285
285
encryptedData .read (initVector );
286
286
encryptedData .read (givenMac );
@@ -290,7 +290,7 @@ private static boolean writeDecryptedFile(Path inputPath, Path outputPath, byte[
290
290
SecretKeySpec skeySpec = new SecretKeySpec (key , ALGORITHM );
291
291
SecretKeySpec macKey = new SecretKeySpec (key , HASH_AlGORITHM );
292
292
293
- // Initialise cipher and HMac
293
+ // Initialise cipher
294
294
Cipher cipher = Cipher .getInstance (CIPHER );
295
295
cipher .init (Cipher .DECRYPT_MODE , skeySpec , iv );
296
296
0 commit comments