1

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ä"

2
  • quoting php.net about "mcrypt_ecb": This function is deprecated and should not be used anymore, see mcrypt_generic() and mdecrypt_generic() for replacements. Commented Jul 17, 2012 at 9:10
  • if i try this i get: ERROR: invalid byte sequence for encoding "UTF8": 0xa3 Commented Jul 18, 2012 at 9:26

1 Answer 1

2

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.

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

1 Comment

The number isnt from the user, so no sql injection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.