How to do AES encryption&decryption in java?

How to do AES encryption&decryption in java?
Photo by Museum of New Zealand - Te Papa Tongarewa / Unsplash

show you the code:

Base64 UrlEncoder for query parameter

the key and iv are configured in properties in base64 format. So I use Base64 Decoder decode them before create SecretKeySpec and IvParameterSpec.

new SecretKeySpec(Base64.getDecoder().decode(aliasKey), "AES"),
new IvParameterSpec(Base64.getDecoder().decode(aliasIv)));

Since I am using AES to encrypt query parameter which will be transport via internet. so I use Base64 UrlEncoder for encrption and UrlDecoder for decryption.

return Base64.getUrlEncoder().encodeToString(cipherText);
byte[] plainText = cipher.doFinal(Base64.getUrlDecoder().decode(encryption));


Subscribe to Post, Code and Quiet Time.

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.