I'm using phpseclib for encrypting data, My MySQL database's encoding is utf8-general-ci.
when I encrypt an string and save it to the table some of characters appears in '?' charachter. This make mistake when I want to decrypt it.
What encoding should I use to have all of the characters?
please help.
If you encrypt your data to a binary string, it can no longer be stored in an UTF8 encoded string, since some binary values/sequences are just not valid UTF8.
Just base64 encode your string (or change the column type to a binary type) before storing it and things should work better.
Related
I used mysql to store the picture and it is stored by longblob.
Now I need use json the transfer the data of longblob.
The json_endcode($data) returns null.
How to do it?
You could try to serialize image data to base64, but it seems to be a bad idea, since images can be really big. You better store it on ftp server and write just links to images to database
Please check this question - Binary Data in JSON String. Something better than Base64
binary data can be encoded into base64 otherwise JSON does not support it
The JSON format natively doesn't support binary data. The binary data
has to be escaped so that it can be placed into a string element (i.e.
zero or more Unicode chars in double quotes using backslash escapes)
in JSON.
I'm generating a salt using the openssl_random_pseudo_bytes function in php. However, I am unable to store the output into a PostgreSQL database.
I get an error:
ERROR: invalid byte sequence for encoding "UTF8"
Now I realize the encoding of the database in question is set to UTF8, and that the output of the function doesn't match, but what is the correct way of solving this?
This is a random salt, and I'm worried that converting it to UTF8 via whatever process will not make it cryptographically safe anymore?
The openssl_random_pseudo_bytes function outputs binary data, and I was trying to store the output in a database field of data type character varying, mistakenly treating it like a string.
bytea is the correct data type for the field in the PostgreSQL database.
http://www.postgresql.org/docs/9.2/interactive/datatype-binary.html
Update: Also, it seems you need to use pg_escape_bytea on the output to be able to actually insert the binary data into the database.
Thanks to deceze, lechlukasz, and VolkerK in the comments for providing the answer.
I am serializing alot of arrays in php that are to be stored in a database using mysql.
The length of the final string can vary greatly from anything inbetween 2000 to 100,000+, I was wondering what would the best column type for this to be?
I currently have it set as LONGTEXT but I feel this is overkill! The database is already active and has around 3million rows this is a new column which will added soon.
Thanks
Always use any BLOB data-type for serializing data so that it does not get cut off and break the serialization in a binary safe manner. If there is not a maximum to the length of the final string then you will need LONGBLOB. If you know that the data won't fill 2^24 characters you could use a MEDIUMBLOB. MEDIUMBLOB is about 16MB while LONGBLOB is about 4GB so I would say you're pretty safe with MEDIUMBLOB.
Why a binary data type? Text data types in MySQL have an encoding. Character encoding will have an effect on how the serialized data is transposed between the different encodings. E.g. when stored as Latin-1 but then read out as UTF-8 (for example because of the database driver connection encoding setting), the serialized data can be broken because binary offsets did shift however the serialized data was not encoded for such shifts. PHP's serialized strings are binary data, not with any specific encoding.
You should choose BLOB (as Marc B noted) per the PHP manual for serialize():
"Note that this [outputs] a binary string which may include null bytes, and needs to be stored and handled as such. For example, serialize() output should generally be stored in a BLOB field in a database, rather than a CHAR or TEXT field."
Source: http://php.net/serialize
Of course J.Money's input regarding sizes must be borne in mind as well - even BLOB has its limits, and if you are going to exceed them then you would need MEDIUMBLOB or LONGBLOB.
I am having a little issue with storing mcrypt_module_open('rijndael-256','','ofb',''); in a MySQL db.
When it inserts the encrypted data into the MySQL db it looks like this ˜9ÏÏd‰.
It should look like this
÷`¥¶Œ"¼¦q…ËoÇ
I am wondering if I have to do something to get it to work?
Use a blob field type for storing binary data (BLOB, VARBINARY, BINARY)
If you're not doing this already: escape your values with the proper methods if you're using them directly in a SQL-statement. Or even better: use query parameters/prepared statements.
As a last resort you could just encode your data with either base64_encode or bin2hex.
If you want to display binary data on the console or in the browser (even for debugging purpose) use one of those encodings too. Otherwise you might not see the actual data because the browser might not display your binary correctly.
In general, it might be a good idea to base64 encode and decode binary data like this. See Best way to use PHP to encrypt and decrypt passwords? .
Have you tried to Collation of your table that your character supports.
The characters '÷`¥¶Œ"¼¦q…ËoÇ' looks like UTF-8 or someother charset, find charset of your characters and update table Collation based your charset
So I have been browsing the internet, and came across the MySQL built-in function AES_ENCRYPT. It doesn't seem too hard to use, but some sources tell me to store the encrypted data as a VARCHAR, and some say to store it as a BLOB. What should I store the encrypted data as?
Many encryption and compression functions return strings for which the result might contain arbitrary byte values. If you want to store these results, use a column with a VARBINARY or BLOB binary string data type. This will avoid potential problems with trailing space removal or character set conversion that would change data values, such as may occur if you use a nonbinary string data type (CHAR, VARCHAR, TEXT).
Source: http://dev.mysql.com/doc/refman/5.5/en/encryption-functions.html
If you need to use VARCHAR, rather than BLOB, then convert the encrypted binary to Base64 which only uses printable characters and can be safely stored as VARCHAR. Of course you will need to convert it back from Base64 to binary before decrypting.
I have always used blobs to stored encrypted data in MySQL.
You can use Binary. BINARY in STRING. It have to work. I am using it. Write me answer if it doesn't working.