当我尝试使用PostgreSQL的gnupg扩展和gpg_pub_decrypt函数解密时,我收到错误。
这是我正在执行的查询,它应该对字符串Hello world!
进行往返加密/解密:
select pgp_pub_decrypt(
pgp_pub_encrypt('Hello world!', dearmor(:public_key)),
dearmor(:private_key),
''
);
相反,我得到错误:
'SQLSTATE[39000]: External routine invocation exception: 7 ERROR: Corrupt data'
我执行查询并通过这个php脚本提供公钥和私钥值。
<?php
function roundTrip() {
$pubkey = file_get_contents("public_key");
$privkey = file_get_contents("secret_key");
$conn = new PDO("pgsql:dbname=*********", "*********", "*********");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "select pgp_pub_decrypt( pgp_pub_encrypt('Hello world!', dearmor(:public_key)), dearmor(:private_key), '')";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':public_key', $pubkey);
$stmt->bindParam(':private_key', $privkey);
$stmt->execute(); // <------------------ Error encountered here
$row = $stmt->fetch();
file_put_contents('test.out', $row[0]);
}
roundTrip();
?>
我使用gnupg生成了密钥对,并且没有密码保护,因此pgp_pub_decrypt
的最后一个参数是空字符串。
我已将私钥和公钥都导出到ASCII文件以供脚本读取。对于想要验证其有效性的任何人,它们都包括在内。
我可以使用未导出的密钥在命令行上使用gpg成功进行往返,所以我知道它们有效。如果gpg提供了一种方法,我会使用导出的,但我不认为它会这样做。
有谁能告诉我出了什么问题?
[mike@M5 ~]$ cat public_key
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.4.11 (GNU/Linux)
mQENBE86dZEBCADK0LCqLYUPWRCfiBt3qx7OKJZp1QTkwG+sFM5COw2XV96poCwS
7LtwdjiG6+uQ2Ikh6XmRkAoPqR3NHuoI1BXTAaHVJ1xuHXtEsaVdx1hk8yjtDO9Z
eAlWKc84Mq95ZMY7fvphY4CIOnWyMaUxSBNMSFJ6jyTUR712C+kz2oER1ZBeA0y8
bJ9mSii+yOO3ST+uws5M0G/wR+6t033iScbSNftvrMQ+O7wo3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8nd2LFgxEOIgmvZsDlzC0
yanTNBDdzqNAEkVg5/9ZmmMB/nP1Ii2ALQxrmwNka+U9e9S5c0jxdTl6eh//Yz5R
rMXMj6+pIqitpyZlYP1LigROllhAYnc9RGp9R87fgsZBq3Own/NWymdoElGe/IZK
CnBtdEeo7FcXUgEdi0XsklNh5OGpMPbLRjosIf/6dn3LeIQvREshB0GegGMo7CP7
SEZuNGUz6H4rqYSpt0Yr8BVvU++UiQEfBBgBAgAJBQJPOnWRAhsMAAoJEJqY5Ri8
OwtBEZ8H/jTDb2Xt87/SXf7bVzHL2DYNWZ0tO9Kg01AD9nsfSK2E3GdTMbTJGvMr
4qC4Bk8uGFEskv9QcDbwUX8kFJdaFyVq/GXkMo2Ax7dwKtFS3J8ZJwhrWW5df9yp
G4O8620CMF9YopVoUWxMXlHwarl2xGGcdZdhfhg2ujYvJLQmne0r7yoNIPjNHIc5
j5774kJv//9wYzwfDXiG5LBtzb7XrFJg4g2sF9sprodHCD3wfgZU2MOCuCvk9Hfe
9YDXjBdXHrXRwDf75bA5h9KcdOSQYUYA2mxC7TCwfknTJo7sVJmZrEYWeFCupWDD
7amHo1k9oeSHosBycjGXNV6nHYqTj88=
=igkq
-----END PGP PRIVATE KEY BLOCK-----
[mike@M5 ~]$
答案 0 :(得分:0)
根据here,有一个错误,当你提供密码时它会失败。尝试做:
pgp_pub_decrypt( pgp_pub_encrypt( 'Hello world', dearmor(:public_key) ),
dearmor(:private_key) ) `
并且不提供pgp_pub_decrypt
函数中的第三个字段。