Skip to content

Commit 227683d

Browse files
committed
feat: can specify algorithm and key length during encryption
fix #23
1 parent 891efdd commit 227683d

File tree

2 files changed

+78
-11
lines changed

2 files changed

+78
-11
lines changed

src/FileEncryptor.java

Lines changed: 77 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,36 +34,98 @@
3434
public class FileEncryptor {
3535
private static final Logger LOG = Logger.getLogger(FileEncryptor.class.getSimpleName());
3636

37-
private static final String ALGORITHM = "AES";
37+
private static final String DEFAULT_ALGORITHM = "AES";
38+
private static final int DEFAULT_KEY_LENGTH = 128;
3839
private static final String HASH_AlGORITHM = "HmacSHA256";
39-
private static final String CIPHER = "AES/CBC/PKCS5PADDING";
40+
private static final String DEFAULT_CIPHER = "AES/CBC/PKCS5PADDING";
4041
private static final int ITERATION_COUNT = 100000;
4142

43+
private static String ALGORITHM;
44+
private static String CIPHER;
45+
private static int KEY_LENGTH;
46+
private static int BLOCKSIZE;
47+
48+
// Error Message
49+
private static final String validCmdMsg = "Valid Encryption commandL: java FileEncryptor enc [Passoword] [inputFile] [outputFile]\n"
50+
+ "Valid Encryption command: java FileEncryptor enc [Algorithm] [Key length] [Password] [inputFile] [outputFile]\n"
51+
+ "Valid Decryption command: java FileEncryptor dec [Password] [inputFile] [outputFile]\n"
52+
+ "Valid Info command: java FileEncryptor info [filePath]\n"
53+
+ "Valid Key lengths: 128. 448. 192. 32 etc"
54+
+ "NOTE: The only algorithms accepted are AES and Blowfish\n"
55+
+ "NOTE: Must specify a valid Key length (in bits) with respect to the algorithm specified\n"
56+
+ "NOTE: The default Algorithm being used is " + DEFAULT_ALGORITHM + " and the Default Key Length is " + DEFAULT_KEY_LENGTH;
57+
4258
public static void main(String[] args) throws Exception {
43-
// Error Message
44-
final String validCmdMsg = "Valid Encryption command: java FileEncryptor enc [Password] [inputFile] [outputFile]\n"
45-
+ "Valid Decryption command: java FileEncryptor dec [Password] [inputFile] [outputFile]\n";
4659

47-
if (args.length < 4) { throw new IllegalArgumentException("Not Enough Argunments specified\n" + validCmdMsg); }
60+
if (args.length < 2) { throw new IllegalArgumentException("Not Enough Argunments specified\n" + validCmdMsg); }
4861

4962
// Convert String arguments to char arrays
5063
char[][] charArgs = Util.getCharArguments(args);
5164

5265
// Clear String argunments
5366
Arrays.fill(args, null);
5467

68+
if (Arrays.equals(charArgs[0], "info".toCharArray())) {
69+
info(new String(charArgs[1]));
70+
return;
71+
}
72+
73+
if (charArgs.length < 4) { throw new IllegalArgumentException("Not Enough Argunments specified\n" + validCmdMsg); }
74+
5575
// Options Available
5676
char[] enc = "enc".toCharArray();
5777
char[] dec = "dec".toCharArray();
5878

5979
if (!Arrays.equals(charArgs[0], enc) && !Arrays.equals(charArgs[0], dec)) {
60-
throw new IllegalArgumentException("Neither enc (encrypt) or dec (decrypt) option specified\n" + validCmdMsg);
80+
throw new IllegalArgumentException("Neither enc (encrypt), dec (decrypt) or info option specified\n" + validCmdMsg);
6181
}
6282

6383
if (Arrays.equals(charArgs[0], enc)) { // Encrypt
64-
encrypt(charArgs[1], new String(charArgs[2]), new String(charArgs[3]));
84+
85+
char[] aes = "AES".toCharArray();
86+
char [] blowfish = "Blowfish".toCharArray();
87+
88+
int argIndex = 1; // will get incremented everytime a valid argument is encountered
89+
90+
// If no algorithm argument is specified the Default will be applied
91+
if (Arrays.equals(charArgs[argIndex], aes) || Arrays.equals(charArgs[argIndex], blowfish)) {
92+
ALGORITHM = new String(charArgs[1]);
93+
CIPHER = ALGORITHM + "/CBC/PKCS5PADDING";
94+
argIndex++;
95+
} else {
96+
ALGORITHM = DEFAULT_ALGORITHM;
97+
CIPHER = DEFAULT_CIPHER;
98+
}
99+
100+
// Determine blocksize for the IV
101+
if (ALGORITHM.equals("AES")) { BLOCKSIZE = 128; }
102+
if (ALGORITHM.equals("Blowfish")) { BLOCKSIZE = 64; }
103+
104+
// If no Key length specified then the Default will be applied
105+
try {
106+
int keyLength = Integer.parseInt(new String(charArgs[argIndex]));
107+
if (keyLength % 8 != 0) { throw new IllegalArgumentException("Invalid Key Length: not divisible by 8"); }
108+
109+
if (ALGORITHM.equals("AES") && keyLength != 128 && keyLength != 192 && keyLength != 256) {
110+
throw new IllegalArgumentException("Invalid Key Length for AES Algorithm, valid key lengths are 128, 192 or 256 bits");
111+
}
112+
113+
if (ALGORITHM.equals("Blowfish") && (keyLength < 32 || keyLength > 448)) {
114+
throw new IllegalArgumentException("Invalid Key Length for Blowfish Algorithm, valid key lengths are between 32-448 bits");
115+
}
116+
117+
KEY_LENGTH = keyLength;
118+
119+
argIndex++;
120+
} catch (NumberFormatException e) {
121+
KEY_LENGTH = DEFAULT_KEY_LENGTH;
122+
}
123+
124+
encrypt(charArgs[argIndex], new String(charArgs[argIndex + 1]), new String(charArgs[argIndex + 2]));
65125

66126
} else if (Arrays.equals(charArgs[0], dec)) { // Decrypt
127+
ALGORITHM = DEFAULT_ALGORITHM;
128+
CIPHER = DEFAULT_CIPHER;
67129
decrypt(charArgs[1], new String(charArgs[2]), new String(charArgs[3]));
68130

69131
}
@@ -97,13 +159,14 @@ public static void main(String[] args) throws Exception {
97159
public static void encrypt(char[] password, String inputPath, String outputPath) throws NoSuchAlgorithmException,
98160
NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IOException, InvalidKeySpecException {
99161
//Generate vector and salts
100-
final byte[] initVector = new byte[16], salt = new byte[16], macSalt = new byte[16];
162+
final byte[] initVector = new byte[BLOCKSIZE/8];
163+
final byte[] salt = new byte[16], macSalt = new byte[16];
101164

102165
SecureRandom sr = new SecureRandom();
103166
sr.nextBytes(initVector); sr.nextBytes(salt); sr.nextBytes(macSalt);
104167

105168
// Get Keys from password
106-
final byte[] key = generateKey(password, salt, 128);
169+
final byte[] key = generateKey(password, salt, KEY_LENGTH);
107170
final byte[] macKey = generateKey(password, macSalt, 256);
108171

109172
// Password no longer needed
@@ -283,6 +346,10 @@ private static boolean writeDecryptedFile(Path inputPath, Path outputPath, char[
283346
return true;
284347
}
285348

349+
private static void info(String filepath) {
350+
System.out.println("INVOKED INFO");
351+
}
352+
286353
/**
287354
* Generates a Secret key with a specified password. The password is added with
288355
* a salt and iterated multiple times before being hased to increase entropy.

src/resources/plaintext.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
This is part three of the assignment
1+
This is part four of the assignment

0 commit comments

Comments
 (0)