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约定使用非对称加密通道进行通讯,具体 ...
随机推荐
- Django (十一) 项目部署 2
阿里云项目部署 ( 如果xshell连接不上阿里云: 解决方法: 1, 在淘宝IP地址库查看当前IP: http://ip.taobao.com/ 2, 点击进入:安全(云盾) -> 安骑士(服 ...
- PHP全国省市区地址分割提取脚本程序
github地址: https://github.com/zmxfree/addressapart 比如将 浙江省杭州市江干区XX路X号 分割成 浙江省 杭州市 江干区 XX路X号,方便excel操作 ...
- linux-ubuntu下调出中文输入法
linux-ubuntu下调出中文输入法 注:此方法我并没有亲身实践过,只是觉得可能会用到,所以保存一下
- 基于 opencv图像去噪
-------------------开通头条号-------------------- 实验名称 图像去噪 实验目的 1.掌握算术均值滤波器.几何均值滤波器.谐 ...
- 字典(dict),增删改查,嵌套
一丶字典 dict 用{}来表示 键值对数据 {key:value} 唯一性 键 都必须是可哈希的 不可变的数据类型就可以当做字典中的键 值 没有任何限制 二丶字典的增删改查 1.增 dic[k ...
- It is not the destination so much as the journey, they say.
It is not the destination so much as the journey, they say. 人家说目的地不重要,重要的是旅行的过程.<加勒比海盗>
- 草根程序员如何进入BAT
首页 最新文章 IT 职场 前端 后端 移动端 数据库 运维 其他技术 - 导航条 - 首页 最新文章 IT 职场 前端 - JavaScript - HTML5 - CSS 后端 - Pyt ...
- SQL必知必会-笔记
一.数据库/数据表 数据库(DATABASE):存储有组织的数据的容器; 数据库管理系统(DBMS):数据库软件.开发者通过 DBMS 操纵 DATABASE 表(TABLE):表是一种结构化的文件, ...
- Android Doze模式源码分析
科技的仿生学无处不在,给予我们启发.为了延长电池是使用寿命,google从蛇的冬眠中得到体会,那就是在某种情况下也让手机进入类冬眠的情况,从而引入了今天的主题,Doze模式,Doze中文是打盹儿,打盹 ...
- java object默认的基本方法
java object默认的基本方法中没有copy(),含有如下9个方法: getClass(), hashCode(), equals(), clone(), toString(), notify ...