I am trying to decrypt the string \"~9?8?m???=?T?G\"
that I receive from a back-end server which uses OpenSSL to encrypt the String using AES-256-CBC. There is
Keep the following things in mind while encrypting and decrypting strings,
ENCRYPTION:
Convert string to byteArray using toByteArray(Charsets.UTF-8) and always specify the charset with UTF-8.
Encrypyt the above byteArray using cipher.doFinal(byteArray).
Now this is not enough, you need to do base64 encoding for that encrypted byteArray using Base64.encode(encryptedByteArray, Base64.DEFAULT)
Remember this again returns byteArray , if you want to convert to string, use toString(Charsets.UTF-8) and most importantly specify the charset again as UTF-8 and then process or store it in DB as you wish.
DECRYPTION:
1.Get the encrypted string and first step while decrypting is to decode the encrypted string using base64.decode(encryptedString.toByteArray(Charsets.UTF-8), Base64.DEFAULT)
Now decrypt the decoded byteArray by using cipher.dofinal(decodedByteArray).
Convert the Decrypted byteArray to String using toString(Charsets.UTF-8). NOTE:Always specify the charset. This returns the original string.
I know I have not shared any code but trust me the flow is the important part while encrypting and decrypting a string..