[英]How do you use the PHP OpenPGP library?
There is a PHP extension port of the gnupg PGP library. gnupg PGP 库有一个PHP 扩展端口。 However, I'm having a hard time finding examples that explain how to use the library.
但是,我很难找到解释如何使用该库的示例。
How do you properly create keys for application users, and then use them to encrypt/decrypt text using the GnuPG library?您如何为应用程序用户正确创建密钥,然后使用 GnuPG 库使用它们来加密/解密文本?
See this URL it is very help full to you.看到这个 URL 对你很有帮助。 Download example and try it.
下载示例并尝试。
https://github.com/singpolyma/openpgp-php https://github.com/singpolyma/openpgp-php
Or Try it:-或者试试看:-
You can download lib/openpgp.php and lib/openpgp_crypt_rsa.php files in above the URL.您可以在 URL 上方下载lib/openpgp.php和lib/openpgp_crypt_rsa.php文件。
examples/keygen.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例子/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例子/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));
?>
They are very good examples based on PHP extension port has you have requested and we would take a look at some examples它们是基于PHP 扩展端口的非常好的示例,您是否已请求,我们将看一些示例
Using GnuPG with PHP -- Full Tutorials在 PHP 中使用 GnuPG -- 完整教程
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());
}
You should also look at the Example您还应该查看示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.