Search code examples
phpmysqlencryptionpdomcrypt

can not insert MCRYPT encrypted data to the database


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();

Solution

  • additionally encode base64

    encode after encryption

    base64_encode($encrypted)
    

    and decode before decryption

    base64_decode($encrypted)