Skip to content

Commit f59a3c2

Browse files
committed
feat: info command allows user to query metadata
1 parent 23052ee commit f59a3c2

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/FileEncryptor.java

Lines changed: 23 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
}
@@ -355,7 +358,26 @@ private static boolean writeDecryptedFile(Path inputPath, Path outputPath, char[
355358
}
356359

357360
private static void info(String filepath) {
358-
System.out.println("INVOKED INFO");
361+
if (!filepath.contains(".enc")) { throw new IllegalArgumentException("Invalid file requested must be an encrypted file e.g. encrypted.enc"); }
362+
363+
try (InputStream fin = new FileInputStream(new File(filepath))) {
364+
BLOCKSIZE = fin.read(); KEY_LENGTH = fin.read() * 8; int algoLength = fin.read();
365+
ALGORITHM = new String(fin.readNBytes(algoLength));
366+
367+
final byte[] initVector = new byte[BLOCKSIZE/8], salt = new byte[16], macSalt = new byte[16], givenMac = new byte[32];
368+
fin.read(initVector); fin.read(salt); fin.read(macSalt); fin.read(givenMac);
369+
370+
System.out.println("\nMetadata for file: " + filepath);
371+
372+
System.out.print("\n<---------------------------------------->\n");
373+
System.out.print("Algorithm: " + ALGORITHM + "\nKey length: " + KEY_LENGTH + "\nBlocksize: " + BLOCKSIZE);
374+
375+
displayInformation(getPair("Init Vector", initVector), getPair("Salt", salt),
376+
getPair("Mac salt", macSalt), getPair("Computed Mac", givenMac));
377+
378+
} catch (IOException e) {
379+
LOG.warning("Please enter a valid filepath");
380+
}
359381
}
360382

361383
/**

0 commit comments

Comments
 (0)