user1010775
user1010775

Reputation: 371

Encrypt data in postgres with php

I need to store a number encrypted in my postgres database. I wanted to use mcrypt with the 3DES function, the encrypting and decrypting is working fine, but I can't store it in the database. My database field is char(50).

$key = "this is a secret key";
$input = "123456789";

$test = mcrypt_ecb(MCRYPT_3DES, $key, $input, MCRYPT_ENCRYPT);
$db = pg_connect("host=localhost dbname=testdb user=haxo");
$sql = "insert into test (pin) values('".$test."')";
$result = pg_query($sql); 
if (!$result) {
    $errormessage = pg_last_error();
    echo "Error with query: " . $errormessage;
    exit();
} 
pg_close(); 

The error I'm getting is: ERROR: unterminated quoted string at or near "'Ÿlä"

Upvotes: 1

Views: 1170

Answers (1)

maniek
maniek

Reputation: 7307

Make the field type BYTEA (which is for storing binary strings), then use something like PDO prepare, bindValue, execute to insert the values.

also, do You know about sql injection? The coding pattern You are using is a simple recipe for trouble.

Upvotes: 2

Related Questions