Skip to content

Commit c207690

Browse files
authored
Merge pull request #28 from modothprav/hotfix
Hotfix - for recent release, updated spelling, documentation and refactored code
2 parents 3693df6 + 1e6861b commit c207690

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

src/FileEncryptor.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static void main(String[] args) throws Exception {
4545
if (args.length < 1) { throw new IllegalArgumentException("Not Enough Argunments specified\n" + validCmdMsg); }
4646

4747
// Convert String arguments to char arrays
48-
char[][] charArgs = Util.getCharArgunments(args);
48+
char[][] charArgs = Util.getCharArguments(args);
4949

5050
// Clear String argunments
5151
Arrays.fill(args, null);
@@ -56,9 +56,7 @@ public static void main(String[] args) throws Exception {
5656
return;
5757
}
5858

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 ); }
6260

6361
// Options Available
6462
char[] enc = "enc".toCharArray();
@@ -68,7 +66,7 @@ public static void main(String[] args) throws Exception {
6866
throw new IllegalArgumentException("Neither enc (encrypt) or dec (decrypt) option specified\n" + validCmdMsg);
6967
}
7068

71-
byte[] key;
69+
byte[] key = new byte[16];
7270
try {
7371
key = Base64.getDecoder().decode(Util.convertCharToByte(charArgs[1]));
7472
} catch (IllegalArgumentException e) {
@@ -113,7 +111,7 @@ public static void encrypt(byte[] key, String inputPath, String outputPath) thro
113111
NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IOException {
114112
//Generate Initilisation Vector
115113
SecureRandom sr = new SecureRandom();
116-
byte[] initVector = new byte[16];
114+
final byte[] initVector = new byte[16];
117115
sr.nextBytes(initVector); // 16 bytes IV
118116

119117
// Initialize Vector and Keys
@@ -136,7 +134,7 @@ public static void encrypt(byte[] key, String inputPath, String outputPath) thro
136134

137135
// Compute Mac for authentication
138136
hmac.update(initVector);
139-
byte[] mac = computeMac(hmac, plaintextFile);
137+
final byte[] mac = computeMac(hmac, plaintextFile);
140138

141139
// Display the Base64 encoded versions of Vector and computed mac
142140
System.out.print("\n<---------------------------------------->\n");
@@ -206,7 +204,8 @@ private static byte[] computeMac(Mac hmac, Path filePath) {
206204
hmac.update(bytes, 0, length);
207205
}
208206
} catch (IOException e) {
209-
207+
LOG.log(Level.SEVERE, "IOException caught - Please check filepath specified");
208+
System.exit(0);
210209
}
211210

212211
return hmac.doFinal();
@@ -275,12 +274,13 @@ public static void decrypt(byte[] key, String inputPath, String outputPath) thro
275274
* @throws InvalidKeyException
276275
* @throws InvalidAlgorithmParameterException
277276
*/
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 {
279279
try (InputStream encryptedData = Files.newInputStream(inputPath);){
280280

281281
// 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];
284284

285285
encryptedData.read(initVector);
286286
encryptedData.read(givenMac);
@@ -290,7 +290,7 @@ private static boolean writeDecryptedFile(Path inputPath, Path outputPath, byte[
290290
SecretKeySpec skeySpec = new SecretKeySpec(key, ALGORITHM);
291291
SecretKeySpec macKey = new SecretKeySpec(key, HASH_AlGORITHM);
292292

293-
// Initialise cipher and HMac
293+
// Initialise cipher
294294
Cipher cipher = Cipher.getInstance(CIPHER);
295295
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
296296

src/Util.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static byte[] convertCharToByte(char[] chars) {
4141
* @param args String[] An array of strings
4242
* @return char[][] A 2-Dimensional character array
4343
*/
44-
public static final char[][] getCharArgunments(String[] args) {
44+
public static final char[][] getCharArguments(String[] args) {
4545
char[][] charArgs = new char[args.length][];
4646
for (int i = 0; i < args.length; i++) {
4747
charArgs[i] = args[i].toCharArray();

0 commit comments

Comments
 (0)