使用 PHP "decryption failed"进行 PGP 加密



我正在尝试用php加密和解密文件。应该使用pgp异步解密这些文件。

当我使用默认的gnupg-php函数时,加密效果很好。我可以在控制台上破译它。如果我试图用php解密它,我总是会得到一个不具体的异常:"解密失败">

我也用不同的钥匙试过。我还使用了另一个库"singpolyma/openpgp-php"。

问题出在哪里?

$sMessage = 'Huhu';
$recipient = 'test@email.com';
$sPassword = '1234';
// initialize PGP
putenv("GNUPGHOME=".__DIR__."/.pgp/");
$oPgp = new gnupg();
$oPgp->import($sPublicKey);
$oPgp->seterrormode(GNUPG_ERROR_EXCEPTION);
$oPgp->import($sPublicKey);
$oPgp->addencryptkey($recipient);
$ciphertext = $oPgp->encrypt($sMessage);
echo '<pre>'.$ciphertext.'</pre>';
file_put_contents('/tmp/ciphertext.gpg', $ciphertext);
// create new GnuPG object
$gpg = new gnupg();
$gpg->import($sPrivateKey);
// throw exception if error occurs
$gpg->seterrormode(gnupg::ERROR_EXCEPTION);
// ciphertext message
$gpg->adddecryptkey($recipient, $sPassword);
$plaintext = $gpg->decrypt($ciphertext);
echo '<pre>' . $plaintext . '</pre>';

我解决了我的问题。它是由gnupg v2中的更改引起的。问题是使用了密钥密码。可以通过将pgp配置为无人参与密码使用来解决此问题。看看"yougot@haxed.com"的评论

最新更新