<?php
一:环境安装gpg
https://blog.csdn.net/leyangjun/article/details/83146592
二:安装扩展
下载安装地址:http://pecl.php.net/package/gnupg 下载后解压安装
三:PHP使用gpg解密例子
1:面向对象形式
$gpg = new gnupg(); //如果没有找到gnupg类,new \gnupg()命名空间冲突原因找不到
$gpg->seterrormode(gnupg::ERROR_EXCEPTION);
$publicKeyData = file_get_contents('/User/leyangjun/gpg.leyangjun_public.key');//导入对应的公钥,就是gpp生成的
$publicInfo = $gpg -> import($publicKeyData);
$privateKeyData = file_get_contents('/User/leyangjun/gpg.leyangjun_private.key');//导入私钥用于解密,导入后会自动找该私密解密
$gpg -> import($privateKeyData);
$cipherText = file_get_contents('/User/leyangjun/leyangjun.txt.gpg');//需要解密的文件
$gpg->adddecryptkey($publicInfo['fingerprint'], '');//fingerprint为钥匙指纹
try {
$plaintext = $gpg->decrypt($ciphertext); var_dump($plaintext);
} catch (Exception $e) {
die('ERROR: ' . $e->getMessage());
}
//===========================================================================
2:面向过程形式
$gpg = gnupg_init();
$publicPath = Gpg::getPath('gpg_meitu_public');
$publicKey = file_get_contents('/User/leyangjun/gpg.leyangjun_public.key');//导入公钥
$publicInfo = gnupg_import($gpg,$publicKey);
$privateKey = file_get_contents('/User/leyangjun/gpg.leyangjun_private.key');//导入私钥
gnupg_import($gpg,$privateKey);
$DecryptFile = '/User/leyangjun/leyangjun.txt.gpg';
$cipherText = file_get_contents($DecryptFile);
gnupg_adddecryptkey($publicInfo['fingerprint'], '');
try {
$plainText = gnupg_decrypt($gpg,$cipherText);
var_dump($plainText);
}catch (Exception $e) {
die('ERROR: ' . $e->getMessage());
}
PHP gnupg加密解密
猜你喜欢
转载自blog.csdn.net/leyangjun/article/details/83148337
今日推荐
周排行