This post demonstrates encrypting/decrypting messages by using symmetric algorithms such as AES. It also demonstrates computing MAC(Message Authentication Code) and encoding/decoding messages with Base64.
Encryption Step Highlights
- Generate 128 random bit using SecureRandom
- Encrypt plain text using AES algorithm with a predefined symmetric key and prepend the random bits
- Calculate the MAC(Message Authentication Code) and append it to the cipher
- Encode the whole cipher in Base64 for transmission purpose(i.e. via email or web)
Dencryption Step Highlights
- Decode the received Base64 message back to binary
- Extract out the random padding, encrypted text and MAC
- Validate the MAC to ensure data intercity
- Decrypt the cipher using AES algorithm with the same predefined symmetric key
Note: You may need to install unlimited strength JCE to run this program. The default JCE bundled in Oracle JRE can only support upto 128bit key size.
Please refer to the complete example code below