can not insert MCRYPT encrypted data to the database

211 views Asked by At

I'm using MCRYPT to encrypt sensitive data and save them to the database. The encrypted data look like this (non-encrypted data above the encrypted data)

real data above the encrypted data

then insert to the MySQL PDO database as usual in a text field utf8_unicode_ci but the result is empty most of the times. Sometimes it saves only the first character. So how to save the encrypted value into the database?

$stmt=$db->prepare("insert into TABLE (myData) VALUES (:enc) ");
$stmt->bindParam(':enc',$encrypted);
$stmt->execute();
2

There are 2 answers

0
websky On BEST ANSWER

additionally encode base64

encode after encryption

base64_encode($encrypted)

and decode before decryption

base64_decode($encrypted)
0
Marc Niesewendt On

do you get an exception from pdo??

with this little information i would guess you want to save the data into a varchar field.

You may need to change your varchar field to a varbinary field.