Java Symmetric Encryption/Decryption With Message Authentication Code

SW Notes

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

View original post

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s