使用php对postgres中的数据进行加密


Encrypt data in postgres with php

我需要在postgres数据库中存储一个加密的数字。我想使用带有3DES功能的mcrypt,加密和解密都很好,但我无法将其存储在数据库中。我的数据库字段是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(); 

我得到的错误是:ERROR: unterminated quoted string at or near "'Ÿlä"

使字段类型为BYTEA(用于存储二进制字符串),然后使用PDO prepare、bindValue、execute之类的方法插入值。

另外,你知道sql注入吗?你正在使用的编码模式是一个简单的麻烦配方。