您如何使用PHP OpenPGP库? [英] How do you use the PHP OpenPGP library?

查看:191
本文介绍了您如何使用PHP OpenPGP库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP扩展端口 "http://www.gnupg.org/" rel ="noreferrer"> gnupg PGP库.但是,我很难查找示例来解释如何使用该库.

There is a PHP extension port of the gnupg PGP library. However, I'm having a hard time finding examples that explain how to use the library.

如何为应用程序用户正确创建密钥,然后使用它们通过GnuPG库对文本进行加密/解密?

How do you properly create keys for application users, and then use them to encrypt/decrypt text using the GnuPG library?

推荐答案

请参阅此URL,它对您非常有帮助.下载示例并尝试.

See this URL it is very help full to you. Download example and try it.

https://github.com/singpolyma/openpgp-php

或者尝试一下:-

您可以在URL上方下载 lib/openpgp.php lib/openpgp_crypt_rsa.php 文件.

You can download lib/openpgp.php and lib/openpgp_crypt_rsa.php files in above the URL.

示例/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();

示例/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));

?>

这篇关于您如何使用PHP OpenPGP库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆