简体   繁体   中英

How to store encrypted data in mySQL

I've become overwhelmed in terms of 2 way encryption. There's lots of info but I can't seem to find a solid one to suit my needs.

I have the following in PHP:

function encrypt( $key, $string ){
    $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key))));
    return $encrypted;
}

function decrypt( $key, $encrypted ){
    $decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($encrypted), MCRYPT_MODE_CBC, md5(md5($key))), "\0");
    return $decrypted;
}
//Stripped out code for readability
$encryption_Key = 'password'; //such strong, many vulnerable
$name = encrypt($encryption_Key, $_POST['name']);
$stmt = $conn->prepare("UPDATE Customers SET 
                                    name = ? WHERE blah = ?");
$stmt->bind_param('si', $name, $blah);
$stmt->execute();
$stmt->close();

but looking at mySQL, i get jargon like:

~/�Jꎈ*3��9k��Z�f������ï

and unreadable characters when I decrypt like so:

$name = decrypt($encryption_Key, $name ); 

The field type for the name column is currently:

varchar(200)

I have seen examples where they you use binary or blob as the field type but I'm not having much luck. Some help in the right direction would be greatly appreciated.

Just a suggestion. Have you looked at the collation for the name field in your database? Make sure it is on something like utf8_general_ci . Then be sure to print the encrypted $name variable before updating to database to be sure it contains the correct value. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM