Skip to content

Commit 75e47ed

Browse files
authored
Merge pull request #35 from modothprav/update-mac
Update mac
2 parents f034d52 + 911099d commit 75e47ed

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

src/FileEncryptor.java

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.IOException;
55
import java.io.InputStream;
66
import java.io.OutputStream;
7+
import java.nio.ByteBuffer;
78
import java.nio.file.Files;
89
import java.nio.file.Path;
910
import java.nio.file.Paths;
@@ -42,10 +43,8 @@ public class FileEncryptor {
4243
private static final String DEFAULT_CIPHER = "AES/CBC/PKCS5PADDING";
4344
private static final int ITERATION_COUNT = 100000;
4445

45-
private static String ALGORITHM;
46-
private static String CIPHER;
47-
private static int KEY_LENGTH;
48-
private static int BLOCKSIZE;
46+
private static String ALGORITHM, CIPHER;
47+
private static int KEY_LENGTH, BLOCKSIZE;
4948

5049
// Error Message
5150
private static final String ERROR_MSG = "\nValid Encryption command: java FileEncryptor enc [Passoword] [inputFile] [outputFile]\n"
@@ -78,21 +77,19 @@ public static void main(String[] args) throws Exception {
7877
if (charArgs.length < 4) { throw new IllegalArgumentException("Not Enough Argunments specified\n" + ERROR_MSG); }
7978

8079
// Options Available
81-
char[] enc = "enc".toCharArray();
82-
char[] dec = "dec".toCharArray();
80+
char[] enc = "enc".toCharArray(), dec = "dec".toCharArray();
8381

8482
if (!Arrays.equals(charArgs[0], enc) && !Arrays.equals(charArgs[0], dec)) {
8583
throw new IllegalArgumentException("Neither enc (encrypt), dec (decrypt) or info option specified\n" + ERROR_MSG);
8684
}
8785

8886
if (Arrays.equals(charArgs[0], enc)) { // Encrypt
8987

90-
char[] aes = "AES".toCharArray();
91-
char [] blowfish = "Blowfish".toCharArray();
88+
char[] aes = "AES".toCharArray(), blowfish = "Blowfish".toCharArray();
9289

9390
int argIndex = 1; // will get incremented everytime a valid argument is encountered
9491

95-
// If no or incompatiable algorithm argument is specified the Default will be applied
92+
// If incompatiable or no algorithm argument is specified the Default will be applied
9693
if (Arrays.equals(charArgs[argIndex], aes) || Arrays.equals(charArgs[argIndex], blowfish)) {
9794
ALGORITHM = new String(charArgs[1]);
9895
CIPHER = ALGORITHM + "/CBC/PKCS5PADDING";
@@ -134,7 +131,6 @@ public static void main(String[] args) throws Exception {
134131
} else if (Arrays.equals(charArgs[0], dec)) { // Decrypt
135132
if (charArgs.length > 4) { throw new IllegalArgumentException("Too many arguments specified for decryption" + ERROR_MSG); }
136133
decrypt(charArgs[1], new String(charArgs[2]), new String(charArgs[3]));
137-
138134
}
139135

140136
// Tear Down, clear arrays
@@ -189,8 +185,14 @@ public static void encrypt(char[] password, String inputPath, String outputPath)
189185
final Path plaintextFile = Paths.get(inputPath);
190186
final Path encryptedFile = Paths.get(outputPath);
191187

188+
// Convert int to byte array to feed into Hmac
189+
final byte[] blocksize = ByteBuffer.allocate(8).putInt(BLOCKSIZE).array();
190+
final byte[] keyLength = ByteBuffer.allocate(8).putInt(KEY_LENGTH/8).array();
191+
final byte[] algoLength = ByteBuffer.allocate(8).putInt(ALGORITHM.getBytes().length).array();
192+
192193
// Compute Mac for authentication
193-
final byte[] mac = computeMac(hmac, plaintextFile, initVector, salt, macSalt);
194+
final byte[] mac = computeMac(hmac, plaintextFile, blocksize, keyLength, algoLength,
195+
ALGORITHM.getBytes(), initVector, salt, macSalt);
194196

195197
// Display the Base64 encoded versions of Key, Vector and computed mac
196198
displayInformation(getPair("Secret Key", key), getPair("Init Vector", initVector), getPair("Salt", salt),
@@ -227,8 +229,8 @@ private static boolean writeEncryptedFile(Path inputPath, Path outputPath, Ciphe
227229

228230
try (FileOutputStream fout = new FileOutputStream(outputPath.toFile());) {
229231
// Write Metadata
230-
final byte[] algorithm = Util.convertCharToByte(ALGORITHM.toCharArray());
231-
232+
final byte[] algorithm = ALGORITHM.getBytes();
233+
232234
fout.write(BLOCKSIZE); fout.write(KEY_LENGTH/8); fout.write(algorithm.length);
233235
fout.write(algorithm); fout.write(cipher.getIV()); fout.write(salt);
234236
fout.write(macSalt); fout.write(mac);
@@ -337,7 +339,14 @@ private static boolean writeDecryptedFile(Path inputPath, Path outputPath, char[
337339
// Check authentication and file integerity
338340
Mac hmac = Mac.getInstance(HASH_AlGORITHM);
339341
hmac.init(macKeySpec);
340-
final byte[] computedMac = computeMac(hmac, outputPath, initVector, salt, macSalt);
342+
343+
// Convert int to byte array to feed into mac
344+
final byte[] blocksize = ByteBuffer.allocate(8).putInt(BLOCKSIZE).array();
345+
final byte[] keyLength = ByteBuffer.allocate(8).putInt(KEY_LENGTH/8).array();
346+
final byte[] algoLengthArry = ByteBuffer.allocate(8).putInt(ALGORITHM.getBytes().length).array();
347+
348+
final byte[] computedMac = computeMac(hmac, outputPath, blocksize, keyLength,
349+
algoLengthArry, ALGORITHM.getBytes(), initVector, salt, macSalt);
341350

342351
if (!Arrays.equals(givenMac, computedMac)) {
343352
throw new SecurityException("Authentication failed, file may have been tampered with");

0 commit comments

Comments
 (0)