Skip to content

Commit 107e4ca

Browse files
committed
docs: javadoc updated for several methods
1 parent bdd34c9 commit 107e4ca

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/FileEncryptor.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,11 @@ public static void main(String[] args) throws Exception {
9696
/**
9797
* Encrypts a plain text input file by outputing an encrypted version. It does this
9898
* generating a 128 bit secret key and initialisation vector which are used as the
99-
* specifications during the file encryption process.
99+
* specifications during the file encryption process. A message aithentication code
100+
* is also computed with the intialisaton vector and plaintext values, hence these
101+
* values can be checked for tampering during decryption.
100102
*
103+
* @param key byte[] The secrect key which will be used to encrypt the file
101104
* @param inputPath - A String specifying the Input path of the plaintext file
102105
* @param outputPath - A String specifying the Ouput path of the ciphertext file
103106
* @throws NoSuchAlgorithmException
@@ -259,20 +262,20 @@ public static void decrypt(byte[] key, String inputPath, String outputPath) thro
259262
* The encrypted files gets decrypted and written out to the output file.
260263
* For a successful decryption the Cipher needs to be initialized in DECRYPT mode
261264
* with the correct key and vector specifications. The IV is read from the encrypted
262-
* file as it was saved unencrypted during the encryption process.
265+
* file as it was saved unencrypted during the encryption process. Decryption will
266+
* also fail if the computed authentication code doesn't match with the given
267+
* authentication code, which it also reads from the encrpted file.
263268
*
264269
* @param inputPath Path The input file path (encrypted file)
265270
* @param outputPath Path The output file path (decrypted file)
266-
* @param cipher Cipher The cipher instance initialized with the appropriate
267-
* specifications in DECRYPT mode
271+
* @param key byte[] The secret key which will be used for decryption
268272
* @return boolean True if Decryption is successful False otherwise
269273
* @throws NoSuchPaddingException
270274
* @throws NoSuchAlgorithmException
271275
* @throws InvalidKeyException
272276
* @throws InvalidAlgorithmParameterException
273277
*/
274-
private static boolean writeDecryptedFile(Path inputPath, Path outputPath, byte[] key) throws NoSuchAlgorithmException,
275-
NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException {
278+
private static boolean writeDecryptedFile(Path inputPath, Path outputPath, byte[] key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException {
276279
try (InputStream encryptedData = Files.newInputStream(inputPath);){
277280

278281
// Read metadata from the input file
@@ -290,8 +293,6 @@ private static boolean writeDecryptedFile(Path inputPath, Path outputPath, byte[
290293
// Initialise cipher and HMac
291294
Cipher cipher = Cipher.getInstance(CIPHER);
292295
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
293-
Mac hmac = Mac.getInstance(HASH_AlGORITHM);
294-
hmac.init(macKey);
295296

296297
// Read cipertext data and write plaintext data
297298
try (CipherInputStream decryptStream = new CipherInputStream(encryptedData, cipher);) {
@@ -304,6 +305,9 @@ private static boolean writeDecryptedFile(Path inputPath, Path outputPath, byte[
304305
}
305306

306307
// Check authentication and file integerity
308+
Mac hmac = Mac.getInstance(HASH_AlGORITHM);
309+
hmac.init(macKey);
310+
307311
hmac.update(initVector);
308312
byte[] computedMac = computeMac(hmac, outputPath);
309313
if (!Arrays.equals(givenMac, computedMac)) {
@@ -312,7 +316,6 @@ private static boolean writeDecryptedFile(Path inputPath, Path outputPath, byte[
312316

313317
LOG.info("Authentication passed, file integrity maintained");
314318

315-
316319
} catch (IOException ex) {
317320
Logger.getLogger(FileEncryptor.class.getName()).log(Level.SEVERE, "IOException caught");
318321
return false;

0 commit comments

Comments
 (0)