0

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.

2
  • Your code worked fine for me. Check what your DB and table's encoding are set to. Commented Dec 24, 2014 at 2:16
  • I did resolve it, had to change the collation to utf8_general_ci. finally passed this little hurdle! Commented Dec 24, 2014 at 2:20

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, i did set the collation to what you suggested, found another error in my code and now it works!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.