How to decrypt with AES programmatically
In this section, we are going to develop the decrypt
program that can encrypt a file encrypted by the encrypt
program.
Our decryption program will be similar to the encryption program and will also take three command-line arguments:
- Input file name
- Output file name
- Encryption key, hex-encoded
This time, the input file is the encrypted file created by the preceding encrypt
program.
Let’s make a high-level plan, similar to how we did before:
- Read the IV from the input file.
- Initialize decryption.
- Decrypt chunk by chunk, reading plaintext chunks from the input file and writing the resulting plaintext chunks into the output file.
- Read the authentication tag from the input file and set it into the cipher context.
- Finalize decryption.
As we can see, the decryption plan is very similar to the encryption plan – initalize, process, and finalize. Let’s see how it is implemented...