I am using Mcrypt to encrypt some strings.
After that I store them in my database, but in my database it looks like "??f??R?????h$", because many special chars are replaced by a '?'.
Do I have to use a special charset or is there another simple way?
Regards, Cr41s3
1) To store binary stuff in mysql, use type BINARY/VARBINARY/BLOB instead of CHAR/VARCHAR/TEXT
2) When saving binary data to DB, use a method that won't apply any processing to the 'text' (you don't want to have your binary data processed as it were some string). Best bet is to use prepared statements of mysqli or PDO.
3) If you, for some reason, want to 'see binary stuff in DB', you will need to make them somewhat readable. Like using
SELECT hex(binary_field)
orSELECT TO_BASE64(binary_field)
4) If you refuse to use BLOB and prepared statements, well, to store binary data in mysql, first convert them using base64_encode(), so it will contain only printable characters.
I think you might be saving the encrypted string's bytes directly into mysql database.
You could do something like this to solve your problem:
Encryption: Orignal Text > MCrypt Encrypt > Base64 Encode > Store as Plain Text in MySQL
Decryption: Load encrypted base64 encoded text from MySQL > Base64 Decode > MCrypt Decrypt -> Orignal Text
This is how I would do it. Create a class to do encryption/decryption:
Then use it like this:
This respectively outputs the following:
I now used the method by Ryan Vincent:
I first encode everything with
base64_encode
and then store it in my database.