|
1 | 1 | import java.io.File; |
| 2 | +import java.io.FileInputStream; |
2 | 3 | import java.io.FileOutputStream; |
3 | 4 | import java.io.IOException; |
4 | 5 | import java.io.InputStream; |
|
26 | 27 | import javax.crypto.spec.PBEKeySpec; |
27 | 28 | import javax.crypto.spec.SecretKeySpec; |
28 | 29 |
|
| 30 | + |
29 | 31 | /** |
30 | 32 | * |
31 | 33 | * @author Erik Costlow |
@@ -130,6 +132,7 @@ public static void main(String[] args) throws Exception { |
130 | 132 | encrypt(charArgs[argIndex], new String(charArgs[argIndex + 1]), new String(charArgs[argIndex + 2])); |
131 | 133 |
|
132 | 134 | } else if (Arrays.equals(charArgs[0], dec)) { // Decrypt |
| 135 | + if (charArgs.length > 4) { throw new IllegalArgumentException("Too many arguments specified for decryption" + ERROR_MSG); } |
133 | 136 | decrypt(charArgs[1], new String(charArgs[2]), new String(charArgs[3])); |
134 | 137 |
|
135 | 138 | } |
@@ -354,8 +357,43 @@ private static boolean writeDecryptedFile(Path inputPath, Path outputPath, char[ |
354 | 357 | return true; |
355 | 358 | } |
356 | 359 |
|
| 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 | + */ |
357 | 376 | 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 | + } |
359 | 397 | } |
360 | 398 |
|
361 | 399 | /** |
|
0 commit comments