调试Crypt_GPG的过程

我使用了Crypt_GPG这个开源项目来解密GPG,这是我找到的几个为数不多的PHP的GPG库中比较好的了,但是中间遇到一些问题。

在浏览器端的错误输出信息:

( ! ) Fatal error: Uncaught <table style="border: 1px" cellspacing="0"> <tr><td colspan="3" style="background: #ff9999"> <b>Crypt_GPG_FileException</b>: The 'homedir' &quot;/Library/WebServer/.gnupg&quot; is not readable or does not exist and cannot be created. This can happen if 'homedir' is not specified in the Crypt_GPG options, Crypt_GPG is run as the web user, and the web user has no home directory. in <b>/usr/local/webdata/andy/fanli/citic/Crypt/GPGAbstract.php</b> on line <b>278</b></td></tr> <tr><td colspan="3" style="background-color: #aaaaaa; text-align: center; font-weight: bold;">Exception trace</td></tr> <tr><td style="text-align: center; background: #cccccc; width:20px; font-weight: bold;">#</td><td style="text-align: center; background: #cccccc; font-weight: bold;">Function</td><td style="text-align: center; background: #cccccc; font-weight: bold;">Location</td></tr> <tr><td style="text-align: center;">0</td><td>Crypt_GPG_Engine->__construct(Array)</td><td>/usr/local/webdata/andy/fanli/citic/Crypt/GPGAbstract.php in /usr/local/webdata/andy/fanli/citic/Crypt/GPG/Engine.php on line 541

命令行输出的错误信息:

PHP Fatal error:  Uncaught PEAR_Exception: gpg-agent binary not found. If you are sure the gpg-agent is installed, please specify the location of the gpg-agent binary using the 'agent' driver option. in /usr/local/webdata/andy/fanli/citic/Crypt/GPGAbstract.php on line 278
#0 /usr/local/webdata/andy/fanli/citic/Crypt/GPGAbstract.php(278): Crypt_GPG_Engine->__construct(Array)
#1 /usr/local/webdata/andy/fanli/citic/gpg2.php(9): Crypt_GPGAbstract->__construct()
#2 {main}
  thrown in /usr/local/webdata/andy/fanli/citic/Crypt/GPG/Engine.php on line 601

Fatal error: Uncaught PEAR_Exception: gpg-agent binary not found. If you are sure the gpg-agent is installed, please specify the location of the gpg-agent binary using the 'agent' driver option. in /usr/local/webdata/andy/fanli/citic/Crypt/GPGAbstract.php on line 278
#0 /usr/local/webdata/andy/fanli/citic/Crypt/GPGAbstract.php(278): Crypt_GPG_Engine->__construct(Array)
#1 /usr/local/webdata/andy/fanli/citic/gpg2.php(9): Crypt_GPGAbstract->__construct()
#2 {main}
  thrown in /usr/local/webdata/andy/fanli/citic/Crypt/GPG/Engine.php on line 601

第一个browser中报的错我分析是权限问题,第二个在shell中运行才是其真正的问题所在,根据提示,使用brew安装gpg-agent。(或者直接到Engine.php中找到592行注释掉此处判断gpg-agent是否存在的逻辑,因为我这里不使用gpg-agent也是可以的)

之后我编写代码如下:

require_once './Crypt/GPG.php';

$gpg = new Crypt_GPG();
$key = getKey();
$gpg->addDecryptKey('zhangsanfeng','123456');
$encrypted = file_get_contents('wait_for_decrpyt.txt.gpg');
$data = $gpg->decrypt($encrypted);
echo '<pre>';var_dump($data);exit;

终于解密成功!!!

发布了348 篇原创文章 · 获赞 28 · 访问量 204万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章