RSA非对称加密算法实现过程
RSA非对称加密算法实现过程
非对称加密算法有很多,RSA算法就是其中比较出名的算法之一,下面是具体实现过程
<?php
/**
* 使用openssl实现非对称加密 */
class Rsa
{
/**
* private key
*/
private $_privKey; /**
* public key
*/
private $_pubKey; /**
* the keys saving path
*/
private $_keyPath; /**
* the construtor,the param $path is the keys saving path
*/
public function __construct($path)
{
if(empty($path) || !is_dir($path)){
throw new Exception('Must set the keys save path');
} $this->_keyPath = $path;
} /**
* create the key pair,save the key to $this->_keyPath
*/
public function createKey()
{
$r = openssl_pkey_new();
openssl_pkey_export($r, $privKey);
file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR . 'priv.key', $privKey);
$this->_privKey = openssl_pkey_get_public($privKey); $rp = openssl_pkey_get_details($r);
$pubKey = $rp['key'];
file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR . 'pub.key', $pubKey);
$this->_pubKey = openssl_pkey_get_public($pubKey);
} /**
* setup the private key
*/
public function setupPrivKey()
{
if(is_resource($this->_privKey)){
return true;
}
$file = $this->_keyPath . DIRECTORY_SEPARATOR . 'priv.key';
$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 . 'pub.key';
$puk = file_get_contents($file);
$this->_pubKey = openssl_pkey_get_public($puk);
return true;
} /**
* encrypt with the private key
*/
public function privEncrypt($data)
{
if(!is_string($data)){
return null;
} $this->setupPrivKey(); $r = openssl_private_encrypt($data, $encrypted, $this->_privKey);
if($r){
return base64_encode($encrypted);
}
return null;
} /**
* decrypt with the private key
*/
public function privDecrypt($encrypted)
{
if(!is_string($encrypted)){
return null;
} $this->setupPrivKey(); $encrypted = base64_decode($encrypted); $r = openssl_private_decrypt($encrypted, $decrypted, $this->_privKey);
if($r){
return $decrypted;
}
return null;
} /**
* encrypt with public key
*/
public function pubEncrypt($data)
{
if(!is_string($data)){
return null;
} $this->setupPubKey(); $r = openssl_public_encrypt($data, $encrypted, $this->_pubKey);
if($r){
return base64_encode($encrypted);
}
return null;
} /**
* decrypt with the public key
*/
public function pubDecrypt($crypted)
{
if(!is_string($crypted)){
return null;
} $this->setupPubKey(); $crypted = base64_decode($crypted); $r = openssl_public_decrypt($crypted, $decrypted, $this->_pubKey);
if($r){
return $decrypted;
}
return null;
} public function __destruct()
{
@ fclose($this->_privKey);
@ fclose($this->_pubKey);
} } //====================demo=======================
//以下是一个简单的测试demo,如果不需要请删除
$rsa = new Rsa('ssl-key'); //私钥加密,公钥解密
echo 'source:我是老鳖<br />';
$pre = $rsa->privEncrypt('我是老鳖');
echo 'private encrypted:<br />' . $pre . '<br />'; $pud = $rsa->pubDecrypt($pre);
echo 'public decrypted:' . $pud . '<br />'; //公钥加密,私钥解密
echo 'source:干IT的<br />';
$pue = $rsa->pubEncrypt('干IT的');
echo 'public encrypt:<br />' . $pue . '<br />'; $prd = $rsa->privDecrypt($pue);
echo 'private decrypt:' . $prd;
//========================demo======================
?>
参考链接:RSA非对称加密
RSA非对称加密算法实现过程的更多相关文章
- SSH加密原理、RSA非对称加密算法学习与理解
首先声明一下,这里所说的SSH,并不是Java传统的三大框架,而是一种建立在应用层和传输层基础上的安全外壳协议,熟悉Linux的朋友经常使 用到一 个SSH Secure Shell Cilent的工 ...
- RSA非对称加密算法实现:Python
RSA是1977年由罗纳德·李维斯特(Ron Rivest).阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)一起提出的.当时他们三人都在麻省理工学院工作.RSA ...
- RSA非对称加密算法实现:Golang
RSA是1977年由罗纳德·李维斯特(Ron Rivest).阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)一起提出的.当时他们三人都在麻省理工学院工作.RSA ...
- RSA非对称加密算法实现:C#
RSA是1977年由罗纳德·李维斯特(Ron Rivest).阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)一起提出的.当时他们三人都在麻省理工学院工作.RSA ...
- RSA非对称加密算法实现:Java
RSA是1977年由罗纳德·李维斯特(Ron Rivest).阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)一起提出的.当时他们三人都在麻省理工学院工作.RSA ...
- RSA—非对称加密算法
RSA:非对称加密算法加解密原理如下:已知:p,q,n,e,d,m,c其中:p与q互为大质数,n=p*q 公钥Pk(n,e):加密使用,是公开的 私钥Sk(n,d):解密使用,不公开 c:明文 m:密 ...
- RSA 非对称加密算法简述
RSA概述 首先看这个加密算法的命名.很有意思,它其实是三个人的名字.早在1977年由麻省理工学院的三位数学家Rivest.Shamir 和 Adleman一起提出了这个加密算法,并且用他们三个人姓氏 ...
- .NET Core加解密实战系列之——RSA非对称加密算法
目录 简介 功能依赖 生成RSA秘钥 PKCS1格式 PKCS8格式 私钥操作 PKCS1与PKCS8格式互转 PKCS1与PKCS8私钥中提取公钥 PEM操作 PEM格式密钥读取 PEM格式密钥写入 ...
- 【转载】非对称加密过程详解(基于RSA非对称加密算法实现)
1.非对称加密过程: 假如现实世界中存在A和B进行通讯,为了实现在非安全的通讯通道上实现信息的保密性.完整性.可用性(即信息安全的三个性质),A和B约定使用非对称加密通道进行通讯,具体 ...
随机推荐
- MySql提示:The server quit without updating PID file(…)失败之解决办法(来源网络仅供参考)
1.可能是/usr/local/mysql/data/rekfan.pid文件没有写的权限 解决方法 :给予权限,执行 “chown -R mysql:mysql /var/data” “chmod ...
- Web自动化测试—PO设计模式(一)
前言 很多的测试同学懂得使用selenium进行Web自动化测试, 但是不知道如何去写一个测试框架,或者说是一个容易维护的web自动化项目. 自己写一个最基本的web自动化测试框架需要会什么? 1. ...
- springboot项目实现批量新增功能
这个困扰我一整天东西,终于解决了. 首先是mybatis中的批量新增sql语句. 注意:这里我给的是我需要新增的字段,你们改成你们需要的字段. <insert id="insertBa ...
- css文字与文本相关样式
css文字属性定义文本的字体系列,大小,加粗,风格和变形 font-family 设置字体系列 font-size 设置字体的尺寸 font-style ...
- React日常注意点
1.在定义组件的时候,名字首字母一定要大写 最近刚学,我会持续把自己学习过程中的的注意点在这里逐个列出,敬请等待!
- cocos的Director、Scence、Layer(一)---摘自于官方文档
基本结构图(重要) Director: 有那些作用? OpenGL ES的初始化,场景的转换,游戏暂停继续的控制,世界坐标和GL坐标之间的切换,对节点(游戏元素)的控制,游戏数据的保存调用,屏幕尺寸的 ...
- 微信成为HTML5技术流行的最大推手
很多热点的事件都是厚积薄发,HTML5就是如此.此前iOS和Android系统已经放弃了Flash,这让HTML5有了一个天然的成长基础.而现在手机硬件的提升和HTML5本身的完善,使得基于HTML5 ...
- Python3自动化学习地址
w3社区https://www.w3cschool.cn/python/ 易百教程https://www.yiibai.com/ http://www.cnblogs.com/yangxia-test ...
- Protocol Buffer学习教程之开篇概述(一)
1. Protocol Buffer是什么 Protocol Buffer是google旗下的产品,用于序列化与反序列化数据结构,但是比xml更小.更快.更简单,而且能跨语言.跨平台.你可以把你的数据 ...
- 【转载】Python实现图书馆预约功能
注释: 1,原博主是:http://blog.csdn.net/cq361106306/article/details/42644001# 2,学校是我现在的学校,我最近也在研究这个,所以转了. 3, ...