How to do AES encryption&decryption in java?
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)); |