解密加密字符串时收到解密失败响应。请看我下面的两个功能。我使用的是 PHP 5.9,并且我有一个正确的公钥、私钥和密码。
//-----------------------------------------------------------------------------
//Function Name : Encrypt Data
//Purpose : Check key into database and one the basis of key encrypt post data
function encryptData($apiKey,$postBody){
try {
global $g_e_v,$gpg;
$publicKeyResult = getKey($apiKey,'apikeys_public');
$publicKey = $publicKeyResult->apikeys_public;
//Set Enviroment variable for GNUPG.
putenv("GNUPGHOME=".$g_e_v.".gnupg");
$gpg->seterrormode(gnupg::ERROR_EXCEPTION);
$info = $gpg->import($publicKey);
var_dump($info);
$gpg->addencryptkey($info['fingerprint']);
$encryptData = $gpg->encrypt($postBody);
return $encryptData;
}
catch (Exception $exception) {
echo $exception->getMessage();
}
}
//-----------------------------------------------------------------------------
//Function Name : Decrypt Data
//Purpose : Check key into database and on the basis of key decrypt post data.
function decryptData($apiKey,$postBody){
try {
global $g_e_v,$gpg;
$privateKeyResult = getKey($apiKey,'apikeys_private,apikeys_passphrase');
$privateKey = $privateKeyResult->apikeys_private;
$phrase = $privateKeyResult->apikeys_passphrase;
//Set Enviroment variable for GNUPG.
putenv("GNUPGHOME=".$g_e_v.".gnupg");
$gpg->seterrormode(gnupg::ERROR_EXCEPTION);
$info = $gpg->import($privateKey);
var_dump($info);
$gpg->adddecryptkey($info['fingerprint'],$phrase);
$decryptData = $gpg->decrypt($postBody);
return $decryptData;
}
catch (Exception $exception) {
echo $exception->getMessage();
}
}
我这样称呼它:
$en = encryptData('SGFmQMPtkFlWYM','This is a test');
echo $en;
$postBody = decryptData('SGFmQMPtkFlWYM', $en);
我的输出是:
/home/ubuntu/workspace/API/GlobalFunctions.php:81:
array (size=9)
'imported' => int 0
'unchanged' => int 1
'newuserids' => int 0
'newsubkeys' => int 0
'secretimported' => int 0
'secretunchanged' => int 0
'newsignatures' => int 0
'skippedkeys' => int 0
'fingerprint' => string '51103C105AC9E46F06880CA6A1511AEACC486E40' (length=40)
-----BEGIN PGP MESSAGE----- Version: GnuPG v2.0.22 (GNU/Linux) hQIMA3Sy9vCeHG7RARAAwGXu4fnERB44ZXIITbx/MaW/xKHmJa9kEh4ep76Z3TKT TklqYIUbyvvpUrPsAXUUMHAlAiMCiLHbL1aySexSzQ2DhPHjjxEhW3gSVqjNwUxw EclaMHVsTK16EpePMJogWWfY9i1S+VlSQLaaopyiF85b3id94Ik3Pb8FyZ5QLmia Iw5rHs+lc89snZkn0HQo4G+nW0w5E7O1VZR/ktN/IUgdkKB95DPtS88Dv4CAGisK 21+Tfu3q/a7FVJj8FD0aTc7jM19cp1CaWyPk5k0elm3DEEG2qNw7bA4iy6PjPos1 mRahQtHZFr0WtpqPV52Nqimcha3CDjdzJpI0KP94sOXK7dDG2rRAHbcXgYv+Bei8 z95/OK76InPeJQY7sbf7UEyAqh4baAnTgQkXLfkHdKOlrPGw/N/y7IC2Yf2laMQY Q61mfPQ8ri3fxOaBisP8vaBGQAaqvCgje220WzTc3h+D/wYShPr28ftj68gwC4XD N++r2q6TEtbMhJNziq1RrN6m6tR060UBRjv/STCh1tNPFVecrFo5O8F5H78ag37H lI9Ngt7HSCtUF1W5gyFbRbXn3NVm6cBDmPS8Qmi7mDmmta7fbqtzj/R088BKfekD Gl49KZeNUfbPdeMxwU5V23cvCRzUIhXyxs31j1l5qdqH7HYdvtIOPkEfoJfx+NDS QQGum5038urfPruTf1cIj12sO2MoJFH4dlgPtGgi7CSN1/omtWjXJ8gKVwjVT0B5 F2oztlKKkVUrd551dchqkmnh =2kJj -----END PGP MESSAGE-----
/home/ubuntu/workspace/API/GlobalFunctions.php:108:
array (size=9)
'imported' => int 0
'unchanged' => int 1
'newuserids' => int 0
'newsubkeys' => int 0
'secretimported' => int 0
'secretunchanged' => int 1
'newsignatures' => int 0
'skippedkeys' => int 0
'fingerprint' => string '51103C105AC9E46F06880CA6A1511AEACC486E40' (length=40)
decrypt failed
/home/ubuntu/workspace/API/Test.php:9:null