openssl RSA非对称加密、解密、签名、验签
发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,需要先了解的openssl系列函数openssl_pkey_get_private 从证书中解析获取私钥,以供使用。成功,返回真实的密钥资源标识符(Resource ID),否则返回falseopen
千家信息网最后更新 2025年12月02日openssl RSA非对称加密、解密、签名、验签
需要先了解的openssl系列函数
- openssl_pkey_get_private 从证书中解析获取私钥,以供使用。成功,返回真实的密钥资源标识符(Resource ID),否则返回false
- openssl_pkey_get_public 从证书中解析获取公钥,以供使用。成功,返回真实的密钥资源标识符(Resource ID),否则返回false
- openssl_private_encrypt($data, $encrypted, $privateKeyResourceID, OPENSSL_PKCS1_PADDING)
- //使用私钥key加密数据data并且将结果保存至变量crypted中
- openssl_public_decrypt(base64_decode($encrypted), $decrypted, $publicKeyResourceID, OPENSSL_PKCS1_PADDING)
- //私钥加密的内容通过公钥可用解密出来
getMessage());}加密、解密系列
- 公钥加密 openssl_public_encrypt,私钥解密 openssl_private_decrypt
- 私钥加密 openssl_private_encrypt,公钥解密 openssl_public_decrypt
封装
checkFilePath($publicKeyPath); $this->checkFilePath($privatePath); $this->publicKeyContent = file_get_contents($publicKeyPath); $this->privateKeyContent = file_get_contents($privatePath); if (empty($this->publicKeyContent)) throw new \Exception('Public key is empty'); if (empty($this->privateKeyContent)) throw new \Exception('Private key is empty'); $this->publicKeyResourceID = !empty($this->publicKeyContent) ? openssl_pkey_get_public($this->getPublicKey()) : false; $this->privateKeyresourceID = !empty($this->privateKeyContent) ? openssl_pkey_get_private($this->getPrivatekey()) : false; if ($this->publicKeyResourceID === false) throw new \Exception('解析公钥内容失败'); if ($this->privateKeyresourceID === false) throw new \Exception('解析私钥内容失败'); } /** * 校验文件路径 * @param string $filePath * @throws Exception */ public function checkFilePath(string $filePath) { if (!is_file($filePath)) throw new \Exception($filePath . ' is not a regular file'); if (!file_exists($filePath)) throw new \Exception($filePath . ' is not exists'); } //获取私有key字符串,重新格式化,为保证任何key都可以识别 public function getPrivatekey(): string { $search = [ "-----BEGIN RSA PRIVATE KEY-----", "-----END RSA PRIVATE KEY-----", "\n", "\r", "\r\n" ]; $privateKey = str_replace($search, "", $this->privateKeyContent); //打断字符串为指定数量的字串 return $search[0] . PHP_EOL . wordwrap($privateKey, 64, "\n", true) . PHP_EOL . $search[1]; } /** * * 获取公共key字符串,重新格式化,为保证任何key都可以识别 */ public function getPublicKey() { $search = [ "-----BEGIN PUBLIC KEY-----", "-----END PUBLIC KEY-----", "\n", "\r", "\r\n" ]; $publicKey = str_replace($search, "", $this->publicKeyContent); //打断字符串为指定数量的字串 return $search[0] . PHP_EOL . wordwrap($publicKey, 64, "\n", true) . PHP_EOL . $search[1]; } public function createKey() { $result = openssl_pkey_new();// 生成一个新的私钥和公钥对, if ($result === false) return false; openssl_pkey_export($result, $privateKey);//将key当作PEM编码字符串导出并且将之保存到$privateKey(通过引用传递的)中。 $publicKey = openssl_pkey_get_details($result);//返回包含密钥详情的数组 return array('public_key' => $publicKey["key"], 'private_key' => $this->getPrivatekey()); } //使用私钥加密 public function encryptByPrivateKey(string $data): string { openssl_private_encrypt($data, $output, $this->privateKeyresourceID); return base64_encode($output); } //使用公钥解密 public function decryptByPublicKey(string $data): string { openssl_public_decrypt(base64_decode($data), $output, $this->publicKeyResourceID); return $output; } //使用公钥加密 public function encryptByPublicKey(string $data): string { openssl_public_encrypt($data, $output, $this->publicKeyResourceID); return base64_encode($output); } //使用私钥解密 public function decryptByPrivateKey(string $data): string { openssl_private_decrypt(base64_decode($data), $output, $this->privateKeyresourceID); return $output; } //生成签名 public function generateSignature(string $data, int $signType = OPENSSL_ALGO_SHA1): string { openssl_sign($data, $outSignature, $this->privateKeyresourceID, $signType);//Generate signature return base64_encode($outSignature); } //校验签名 OPENSSL_ALGO_SHA256为RSA2 public function checkSignature(string $originalData, string $signature, int $signType = OPENSSL_ALGO_SHA1): bool { //如果签名正确返回 1, 签名错误返回 0, 内部发生错误则返回-1 $result = openssl_verify($originalData, base64_decode($signature), $this->publicKeyResourceID, $signType); return $result == 1; } public function __destruct() { openssl_free_key($this->publicKeyResourceID); openssl_free_key($this->privateKeyresourceID); }}$rsaObj = new RSA('/home/zrj/.ssh/rsa_public.key', '/home/zrj/.ssh/rsa_private.key');$str = 'Hello world';echo '原始数据:' . $str . PHP_EOL;echo '公钥加密私钥解密如下:' . PHP_EOL;$tmpstr = $rsaObj->encryptByPublicKey($str); //用公钥加密echo '加密后的数据:' . PHP_EOL;echo $tmpstr . PHP_EOL;$tmpstr = $rsaObj->decryptByPrivateKey($tmpstr); //用私钥解密echo '解密结果:' . $tmpstr . PHP_EOL;echo PHP_EOL;echo PHP_EOL;echo '私钥加密公钥解密如下:' . PHP_EOL;$tmpstr = $rsaObj->encryptByPrivateKey($str); //用私钥加密echo '私钥加密后的数据:' . PHP_EOL;echo $tmpstr . PHP_EOL;$tmpstr = $rsaObj->decryptByPublicKey($tmpstr); //用公钥解密echo '公钥解密结果:' . $tmpstr . PHP_EOL;echo PHP_EOL;echo PHP_EOL;$signature = $rsaObj->generateSignature($tmpstr);echo '签名结果为:' . $signature . PHP_EOL;var_dump($rsaObj->checkSignature($tmpstr, $signature));
加密
公钥
字符
字符串
数据
结果
内容
密钥
成功
数量
标识
标识符
格式
证书
资源
错误
保证
生成
原始
函数
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
摄像头服务器性能
我的世界ipad版服务器
福建省佰万网络技术是真的吗
思迅收银系统数据库在哪里
网络技术人员面试题及答案
软件开发逻辑是什么
开源dhcp服务器
大连工业大学数据库系统复试
上海朋利网络技术公司地址
派先生网络技术是做什么的
最新万年历数据库
数据库储存文章
单位网络安全自查报告
山东大学网络安全学院书记
软件开发使用java
聚焦网络安全的图片
碧蓝航线2022最新的服务器
合同软件开发公司
请简述软件开发过程
台湾dns服务器
宣城软件开发培训费用
数据库用户模式
任丘盘古网络技术有限公司
嵌入式软件开发太原
国铁集团网络安全等级是几级
图片怎么传入数据库
客服端与服务器连接处关闭
定制化软件开发协议
网络安全研究生哪个学校好考
软件开发图例