調試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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章