如何使用PHP OpenPGP库?

20
2个回答

14
请查看此URL,它对您非常有帮助。下载示例并尝试使用。 https://github.com/singpolyma/openpgp-php 或者尝试以下方法:
您可以在上述URL中下载lib/openpgp.php和lib/openpgp_crypt_rsa.php文件。
示例:keygen.php
<?php

require dirname(__FILE__).'/../lib/openpgp.php';
require dirname(__FILE__).'/../lib/openpgp_crypt_rsa.php';

$rsa = new Crypt_RSA();
$k = $rsa->createKey(512);
$rsa->loadKey($k['privatekey']);

$nkey = new OpenPGP_SecretKeyPacket(array(
   'n' => $rsa->modulus->toBytes(),
   'e' => $rsa->publicExponent->toBytes(),
   'd' => $rsa->exponent->toBytes(),
   'p' => $rsa->primes[1]->toBytes(),
   'q' => $rsa->primes[2]->toBytes(),
   'u' => $rsa->coefficients[2]->toBytes()
));

$uid = new OpenPGP_UserIDPacket('Test <test@example.com>');

$wkey = new OpenPGP_Crypt_RSA($nkey);
$m = $wkey->sign_key_userid(array($nkey, $uid));

print $m->to_bytes();

examples/sign.php

<?php

require dirname(__FILE__).'/../lib/openpgp.php';
require dirname(__FILE__).'/../lib/openpgp_crypt_rsa.php';

/* Parse secret key from STDIN, the key must not be password protected */
$wkey = OpenPGP_Message::parse(file_get_contents('php://stdin'));
$wkey = $wkey[0];

/* Create a new literal data packet */
$data = new OpenPGP_LiteralDataPacket('This is text.', array('format' => 'u', 'filename' => 'stuff.txt'));

/* Create a signer from the key */
$sign = new OpenPGP_Crypt_RSA($wkey);

/* The message is the signed data packet */
$m = $sign->sign($data);

/* Output the raw message bytes to STDOUT */
echo $m->to_bytes();

?>

examples/verify.php

<?php

require dirname(__FILE__).'/../lib/openpgp.php';
require dirname(__FILE__).'/../lib/openpgp_crypt_rsa.php';

/* Parse public key from STDIN */
$wkey = OpenPGP_Message::parse(file_get_contents('php://stdin'));
$wkey = $wkey[0];

/* Parse signed message from file named "t" */
$m = OpenPGP_Message::parse(file_get_contents('t'));

/* Create a verifier for the key */
$verify = new OpenPGP_Crypt_RSA($wkey);

/* Dump verification information to STDOUT */
var_dump($verify->verify($m));

?>

13
使用在网络上找到的随机PHP库进行加密,该库重新实现了PGP,并且没有任何作者是密码学家,该库显然还处于未完成状态(一半的函数都只有一个//TODO作为它们的主体)。会有什么问题? - Tgr
1
@Tgr:“最大的无知就是对你一无所知的事情进行拒绝,却又拒绝去调查。” 唯一可能存在危险的是调用 createKey(),它来自于 https://github.com/phpseclib/phpseclib/blob/master/phpseclib/Crypt/RSA.php#L576,这个函数调用了什么?OpenSSL。现在 OpenSSL 变成了“在网上找到的随机 PHP 库”?请在发表此类评论之前进行一些研究。 - Fredrick Brennan
在@8chan上,密钥生成实际上并不是加密中唯一可能包含危险错误的部分 - 加密也可以(我认为这是显而易见的)。此外,如果OpenSSL不可用,phpseclib(与openpgp-php不同)会执行自己的密钥生成,并在纯PHP中重新实现实际加密。即使您拥有安全的密码原语,您也可以以不安全的方式应用它。请注意,尽管phpseclib得到了一定程度的审查,但其受到的关注远不及OpenSSL。 - Tgr
1
我遇到了一个错误。致命错误:在/www/openpgp-php-master/vendor/phpseclib/phpseclib/phpseclib/Crypt/RSA.php的2091行上,对非对象调用成员函数equals()。 有什么想法吗? - Manu
1
你可以从Composer中下载"phpseclib"库到你的vendor文件夹中。 - Abid Hussain
显示剩余2条评论

7

这些都是基于你所请求的 PHP扩展端口 的非常好的示例,我们将查看一些示例。

使用 GnuPG 和 PHP -- 完整教程

示例

获取密钥信息

putenv('GNUPGHOME=/home/sender/.gnupg');

// create new GnuPG object
$gpg = new gnupg();

// throw exception if error occurs
$gpg->seterrormode(gnupg::ERROR_EXCEPTION); 

// get list of keys containing string 'example'
try {
  $keys = $gpg->keyinfo('example');
  print_r($info);
} catch (Exception $e) {
  echo 'ERROR: ' . $e->getMessage();
}

加密简单邮件

// set path to keyring directory
// set path to keyring directory
putenv('GNUPGHOME=/home/sender/.gnupg');

// create new GnuPG object
$gpg = new gnupg();

// throw exception if error occurs
$gpg->seterrormode(gnupg::ERROR_EXCEPTION); 

// recipient's email address
$recipient = 'dgar@example.org';

// plaintext message
$plaintext = 
"Dear Dave,\n
  The answer is 42.\n
John";

// find key matching email address
// encrypt plaintext message
// display and also write to file
try {
  $gpg->addencryptkey($recipient);
  $ciphertext = $gpg->encrypt($plaintext);
  echo '<pre>' . $ciphertext . '</pre>';
  file_put_contents('/tmp/ciphertext.gpg', $ciphertext);
} catch (Exception $e) {
  die('ERROR: ' . $e->getMessage());
}

解密邮件

// set path to keyring directory
putenv('GNUPGHOME=/home/recipient/.gnupg');

// create new GnuPG object
$gpg = new gnupg();

// throw exception if error occurs
$gpg->seterrormode(gnupg::ERROR_EXCEPTION); 

// recipient's email address
$recipient = 'dgar@example.org';

// ciphertext message
$ciphertext = file_get_contents('/tmp/ciphertext.gpg');

// register secret key by providing passphrase
// decrypt ciphertext with secret key
// display plaintext message
try {
  $gpg->adddecryptkey($recipient, 'guessme');
  $plaintext = $gpg->decrypt($ciphertext);
  echo '<pre>' . $plaintext . '</pre>';
} catch (Exception $e) {
  die('ERROR: ' . $e->getMessage());
}

您还应查看示例


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接