Skip to content

Commit f034d52

Browse files
authored
Merge pull request #34 from modothprav/info-command
Info command fix #25
2 parents 23052ee + 8177d2b commit f034d52

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

src/FileEncryptor.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import java.io.File;
2+
import java.io.FileInputStream;
23
import java.io.FileOutputStream;
34
import java.io.IOException;
45
import java.io.InputStream;
@@ -26,6 +27,7 @@
2627
import javax.crypto.spec.PBEKeySpec;
2728
import javax.crypto.spec.SecretKeySpec;
2829

30+
2931
/**
3032
*
3133
* @author Erik Costlow
@@ -130,6 +132,7 @@ public static void main(String[] args) throws Exception {
130132
encrypt(charArgs[argIndex], new String(charArgs[argIndex + 1]), new String(charArgs[argIndex + 2]));
131133

132134
} else if (Arrays.equals(charArgs[0], dec)) { // Decrypt
135+
if (charArgs.length > 4) { throw new IllegalArgumentException("Too many arguments specified for decryption" + ERROR_MSG); }
133136
decrypt(charArgs[1], new String(charArgs[2]), new String(charArgs[3]));
134137

135138
}
@@ -354,8 +357,43 @@ private static boolean writeDecryptedFile(Path inputPath, Path outputPath, char[
354357
return true;
355358
}
356359

360+
/**
361+
* Allows the user to query metadata for a given file path. The file path
362+
* specified must point to an encrypted file with a .enc extension The metadata
363+
* for the file must also follow a specific format as shown below.
364+
* Metadata format:
365+
* int BLOCKSIZE
366+
* int KEY LENGTH (in bytes)
367+
* int Algorithm Length
368+
* byte[] Algorithm name
369+
* byte[] IV
370+
* byte[] Salt
371+
* byte[] MacSalt
372+
* byte[] Computed Mac
373+
*
374+
* @param String filepath The file being requested to be display the metadata
375+
*/
357376
private static void info(String filepath) {
358-
System.out.println("INVOKED INFO");
377+
if (!filepath.contains(".enc")) { throw new IllegalArgumentException("Invalid file requested must be an encrypted file e.g. encrypted.enc"); }
378+
379+
try (InputStream fin = new FileInputStream(new File(filepath))) {
380+
BLOCKSIZE = fin.read(); KEY_LENGTH = fin.read() * 8; int algoLength = fin.read();
381+
ALGORITHM = new String(fin.readNBytes(algoLength));
382+
383+
final byte[] initVector = new byte[BLOCKSIZE/8], salt = new byte[16], macSalt = new byte[16], givenMac = new byte[32];
384+
fin.read(initVector); fin.read(salt); fin.read(macSalt); fin.read(givenMac);
385+
386+
System.out.println("\nMetadata for file: " + filepath);
387+
388+
System.out.print("\n<---------------------------------------->\n");
389+
System.out.print("Algorithm: " + ALGORITHM + "\nKey length: " + KEY_LENGTH + "\nBlocksize: " + BLOCKSIZE);
390+
391+
displayInformation(getPair("Init Vector", initVector), getPair("Salt", salt),
392+
getPair("Mac salt", macSalt), getPair("Computed Mac", givenMac));
393+
394+
} catch (IOException e) {
395+
LOG.warning("Please enter a valid filepath");
396+
}
359397
}
360398

361399
/**

0 commit comments

Comments
 (0)