Skip to content

Commit 725c7c3

Browse files
committed
feat: decryption with different algorithms and keylenghts
fix #24 fix #31
1 parent 227683d commit 725c7c3

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

src/FileEncryptor.java

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,21 @@ public class FileEncryptor {
4646
private static int BLOCKSIZE;
4747

4848
// 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"
49+
private static final String ERROR_MSG = "\nValid Encryption command: java FileEncryptor enc [Passoword] [inputFile] [outputFile]\n"
50+
+ "\t\t\t java FileEncryptor enc [Algorithm] [Key length] [Password] [inputFile] [outputFile]\n"
51+
+ "\t\t\t java FileEncryptor enc [Algorithm] [Password] [inputFile] [outputFile]\n"
52+
+ "\t\t\t java FileEncryptor enc [Key length] [Password] [inputFile] [outputFile]\n\n"
53+
+ "Valid Decryption command: java FileEncryptor dec [Password] [inputFile] [outputFile]\n\n"
54+
+ "Valid Info command: java FileEncryptor info [filePath]\n\n"
55+
+ "Valid Key lengths: 128, 448, 192, 32 etc\n\n"
5456
+ "NOTE: The only algorithms accepted are AES and Blowfish\n"
5557
+ "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;
58+
+ "NOTE: The default Algorithm being used is " + DEFAULT_ALGORITHM + " and the Default Key Length is " + DEFAULT_KEY_LENGTH + " bits\n"
59+
+ "NOTE: If no Algorithm or Key length is specifed the Default values will be used\n";
5760

5861
public static void main(String[] args) throws Exception {
5962

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

6265
// Convert String arguments to char arrays
6366
char[][] charArgs = Util.getCharArguments(args);
@@ -70,14 +73,14 @@ public static void main(String[] args) throws Exception {
7073
return;
7174
}
7275

73-
if (charArgs.length < 4) { throw new IllegalArgumentException("Not Enough Argunments specified\n" + validCmdMsg); }
76+
if (charArgs.length < 4) { throw new IllegalArgumentException("Not Enough Argunments specified\n" + ERROR_MSG); }
7477

7578
// Options Available
7679
char[] enc = "enc".toCharArray();
7780
char[] dec = "dec".toCharArray();
7881

7982
if (!Arrays.equals(charArgs[0], enc) && !Arrays.equals(charArgs[0], dec)) {
80-
throw new IllegalArgumentException("Neither enc (encrypt), dec (decrypt) or info option specified\n" + validCmdMsg);
83+
throw new IllegalArgumentException("Neither enc (encrypt), dec (decrypt) or info option specified\n" + ERROR_MSG);
8184
}
8285

8386
if (Arrays.equals(charArgs[0], enc)) { // Encrypt
@@ -87,7 +90,7 @@ public static void main(String[] args) throws Exception {
8790

8891
int argIndex = 1; // will get incremented everytime a valid argument is encountered
8992

90-
// If no algorithm argument is specified the Default will be applied
93+
// If no or incompatiable algorithm argument is specified the Default will be applied
9194
if (Arrays.equals(charArgs[argIndex], aes) || Arrays.equals(charArgs[argIndex], blowfish)) {
9295
ALGORITHM = new String(charArgs[1]);
9396
CIPHER = ALGORITHM + "/CBC/PKCS5PADDING";
@@ -103,6 +106,7 @@ public static void main(String[] args) throws Exception {
103106

104107
// If no Key length specified then the Default will be applied
105108
try {
109+
// Perform Key length checks
106110
int keyLength = Integer.parseInt(new String(charArgs[argIndex]));
107111
if (keyLength % 8 != 0) { throw new IllegalArgumentException("Invalid Key Length: not divisible by 8"); }
108112

@@ -115,17 +119,17 @@ public static void main(String[] args) throws Exception {
115119
}
116120

117121
KEY_LENGTH = keyLength;
118-
119122
argIndex++;
120123
} catch (NumberFormatException e) {
121124
KEY_LENGTH = DEFAULT_KEY_LENGTH;
122125
}
123126

127+
// Check if password and/or file paths have been specified
128+
if (argIndex + 2 >= charArgs.length) { throw new IllegalArgumentException("Not enough arguments specified" + ERROR_MSG); }
129+
124130
encrypt(charArgs[argIndex], new String(charArgs[argIndex + 1]), new String(charArgs[argIndex + 2]));
125131

126132
} else if (Arrays.equals(charArgs[0], dec)) { // Decrypt
127-
ALGORITHM = DEFAULT_ALGORITHM;
128-
CIPHER = DEFAULT_CIPHER;
129133
decrypt(charArgs[1], new String(charArgs[2]), new String(charArgs[3]));
130134

131135
}
@@ -159,8 +163,7 @@ public static void main(String[] args) throws Exception {
159163
public static void encrypt(char[] password, String inputPath, String outputPath) throws NoSuchAlgorithmException,
160164
NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IOException, InvalidKeySpecException {
161165
//Generate vector and salts
162-
final byte[] initVector = new byte[BLOCKSIZE/8];
163-
final byte[] salt = new byte[16], macSalt = new byte[16];
166+
final byte[] initVector = new byte[BLOCKSIZE/8], salt = new byte[16], macSalt = new byte[16];
164167

165168
SecureRandom sr = new SecureRandom();
166169
sr.nextBytes(initVector); sr.nextBytes(salt); sr.nextBytes(macSalt);
@@ -221,7 +224,11 @@ private static boolean writeEncryptedFile(Path inputPath, Path outputPath, Ciphe
221224

222225
try (FileOutputStream fout = new FileOutputStream(outputPath.toFile());) {
223226
// Write Metadata
224-
fout.write(cipher.getIV()); fout.write(salt); fout.write(macSalt); fout.write(mac);
227+
final byte[] algorithm = Util.convertCharToByte(ALGORITHM.toCharArray());
228+
229+
fout.write(BLOCKSIZE); fout.write(KEY_LENGTH/8); fout.write(algorithm.length);
230+
fout.write(algorithm); fout.write(cipher.getIV()); fout.write(salt);
231+
fout.write(macSalt); fout.write(mac);
225232

226233
try (CipherOutputStream cipherOut = new CipherOutputStream(fout, cipher);) {
227234
final byte[] bytes = new byte[1024];
@@ -295,13 +302,14 @@ public static void decrypt(char[] password, String inputPath, String outputPath)
295302
private static boolean writeDecryptedFile(Path inputPath, Path outputPath, char[] password) throws NoSuchAlgorithmException,
296303
NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, InvalidKeySpecException {
297304
try (InputStream encryptedData = Files.newInputStream(inputPath);){
298-
299305
// Read metadata from the input file
300-
final byte[] initVector = new byte[16], salt = new byte[16], macSalt = new byte[16], givenMac = new byte[32];
301-
306+
BLOCKSIZE = encryptedData.read(); KEY_LENGTH = encryptedData.read() * 8; int algoLength = encryptedData.read();
307+
ALGORITHM = new String(encryptedData.readNBytes(algoLength)); CIPHER = ALGORITHM + "/CBC/PKCS5PADDING";
308+
309+
final byte[] initVector = new byte[BLOCKSIZE/8], salt = new byte[16], macSalt = new byte[16], givenMac = new byte[32];
302310
encryptedData.read(initVector); encryptedData.read(salt); encryptedData.read(macSalt); encryptedData.read(givenMac);
303311

304-
final byte[] key = generateKey(password, salt, 128);
312+
final byte[] key = generateKey(password, salt, KEY_LENGTH);
305313
final byte[] macKey = generateKey(password, macSalt, 256);
306314

307315
// Password no longer needed

0 commit comments

Comments
 (0)