外链论坛

 找回密码
 立即注册
搜索
查看: 76|回复: 1

PHP实现RSA加密,解密,加签,验签

[复制链接]

2577

主题

5014

回帖

9957万

积分

论坛元老

Rank: 8Rank: 8

积分
99570725
发表于 2024-7-11 02:31:20 | 显示全部楼层 |阅读模式

公钥用于对数据进行加密,私钥用于对数据进行解密;

私钥用于对数据进行签名,公钥用于对签名进行验证。

封装的RSA代码如下:

class Rsa { /** * private key */ private $_privKey; /** * public key */ private $_pubKey; /** * the keys saving path */ private $_keyPath; public function __construct ($path) { if (empty($path) || !is_dir($path)) { throw new \Exception(Must set the keys save path); } //设置私钥 $this->_keyPath = $path; $file = $this->_keyPath . DIRECTORY_SEPARATOR . rsa_private_key.pem; $prk = file_get_contents($file); $this->_privKey = openssl_pkey_get_private($prk); //设置公钥 $file = $this->_keyPath . DIRECTORY_SEPARATOR . rsa_public_key.pem; $puk = file_get_contents($file); $this->_pubKey = openssl_pkey_get_public($puk); } /** * setup the private key */ public function setupPrivKey () { if (is_resource($this->_privKey)) { return true; } $file = $this->_keyPath . DIRECTORY_SEPARATOR . rsa_private_key.pem; $prk = file_get_contents($file); $this->_privKey = openssl_pkey_get_private($prk); return true; } /** * setup the public key */ public function setupPubKey () { if (is_resource($this->_pubKey)) { return true; } $file = $this->_keyPath . DIRECTORY_SEPARATOR . rsa_public_key.pem; $puk = file_get_contents($file); $this->_pubKey = openssl_pkey_get_public($puk); return true; } /** * @function 私钥加密 * @param $data * @return string|
回复

使用道具 举报

1

主题

1014

回帖

-3

积分

限制会员

积分
-3
发表于 2024-9-6 16:34:26 | 显示全部楼层
谷歌外链发布 http://www.fok120.com/
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

站点统计|Archiver|手机版|小黑屋|外链论坛 ( 非经营性网站 )|网站地图

GMT+8, 2024-10-4 01:21 , Processed in 0.057113 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.