How to Encrypt and Decrypt Data In Java

Using AES encryption to encrypt secret data

Suraj Mishra
Published in
3 min readSep 19, 2022


Photo by Markus Spiske on Unsplash

Originally Published in


  • Encryption and Decryption of Secret data is very common process in building any kind of application.
  • We often need to encrypt some kind of secret data/config value such as db-password, hashing token etc.
  • We can encrypt our data either using symmetric or asymmetric encryption. Usually when we talk about symmetric encryption algorithm we use AES and RSA for asymmetric encryption. In Some cases we also use hybrid approach.
  • AES is faster and can encrypt large data sizes while RSA is suitable for encrypting smaller data sizes.
  • This article mainly discusses about Symmetric encryption with AES.

BouncyCastle Dependency

  • BouncyCastle is a great and popular library that supports all the necessary features that we need to encrypt and decrypt the data.
  • You can get latest version from here.

Encrypt Data

  • Secret Key is something that we would need to protect our input byte array. so that only someone who has access to this secret key can decrypt it. Secret key can be of 128 bits, 192 bits or 256 bits , we are using 192 bits here.