I have a String (stringToEncrypt) , it is encrypted by AES-128 and got a byte[]. I try to write that byte[] to database as a String. An example below :
String encryptedString = new String(aes.encode(stringToEncrypt.getBytes()));
“encode” method gets a byte[] as parameter and returns a byte[]. “aes” object makes the AES-128 encoding process.
The problem is, when i try to write encryptedString to DB, some characters that are written to DB is not the same as “String encryptedString”. I think the problem is about the charset of DB or on my code. Any suggestions please?
Stringis the wrong data type for your problem. If you really need to encode raw bytes to a string, use base64.If it is possible, try to store
byte[]directly into the database.