Domanda

I am encrypting my string using the following php code.

$encryption_key = "mickey";
$value = "ddd";
function encrypt($value)
{
    global $encryption_key;
    if(function_exists("mcrypt_ecb"))
    {
        return mcrypt_ecb(MCRYPT_DES, $encryption_key, $value, MCRYPT_ENCRYPT);
    }
    else return $value;
}

I am storing the encrypted value in the database. This stores "?P??" in the database in column "Encrypt"

However when I run this query

select DES_DECRYPT(Encrypt,"mickey") from test_encrypt 

It gives me

3f503f1b3f1b20

How can I retrieve my original $value from the sql query?

Thanks

È stato utile?

Soluzione

Sounds like a character set issue to me.

Likely you're storing the binary value into a VARCHAR (or other non-binary type), and a character set conversion is being applied, and some of the bytes aren't valid "characters" in the characterset for the column, either that, or, the stored values are being translated on retrieval, and "unknown" encodings are being replaced with question marks.

As a test, you could try using the MySQL HEX() and UNHEX() functions on the binary values, though that will effectively double the size of the strings. There shouldn't be any characterset problem with character strings representing hexadecimal digits. (I don't think there are native base-64 encoding/decoding functions in MySQL.)

Alternatively, you might try storing the encrypted values in a column with a datatype that supports binary data, with no characterset translation, e.g. VARBINARY rather than VARCHAR.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top