php实现非对称加密
<?php
/**
* 使用openssl实现非对称加密
*
* @since 2015-11-10
*/
class Rsa
{
/**
* 私钥
*
*/
private $_privKey;
/**
* 公钥
*
*/
private $_pubKey;
/**
* 保存文件地址
*/
private $_keyPath;
/**
* 指定密钥文件地址
*
*/
public function __construct($path)
{
if (empty($path) || !is_dir($path)) {
throw new Exception('请指定密钥文件地址目录');
}
$this->_keyPath = $path;
}
/**
* 创建公钥和私钥
*
*/
public function createKey()
{
$config = [
"config" => 'D:\wamp\bin\apache\apache2.4.9\conf\openssl.cnf',
"digest_alg" => "sha512",
"private_key_bits" => 4096,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
];
// 生成私钥
$rsa = openssl_pkey_new($config);
openssl_pkey_export($rsa, $privKey, NULL, $config);
file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR . 'priv.key', $privKey);
$this->_privKey = openssl_pkey_get_public($privKey);
// 生成公钥
$rsaPri = openssl_pkey_get_details($rsa);
$pubKey = $rsaPri['key'];
file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR . 'pub.key', $pubKey);
$this->_pubKey = openssl_pkey_get_public($pubKey);
}
/**
* 设置私钥
*
*/
public function setupPrivKey()
{
if (is_resource($this->_privKey)) {
return true;
}
$file = $this->_keyPath . DIRECTORY_SEPARATOR . 'priv.key';
$privKey = file_get_contents($file);
$this->_privKey = openssl_pkey_get_private($privKey);
return true;
}
/**
* 设置公钥
*
*/
public function setupPubKey()
{
if (is_resource($this->_pubKey)) {
return true;
}
$file = $this->_keyPath . DIRECTORY_SEPARATOR . 'pub.key';
$pubKey = file_get_contents($file);
$this->_pubKey = openssl_pkey_get_public($pubKey);
return true;
}
/**
* 用私钥加密
*
*/
public function privEncrypt($data)
{
if (!is_string($data)) {
return null;
}
$this->setupPrivKey();
$result = openssl_private_encrypt($data, $encrypted, $this->_privKey);
if ($result) {
return base64_encode($encrypted);
}
return null;
}
/**
* 私钥解密
*
*/
public function privDecrypt($encrypted)
{
if (!is_string($encrypted)) {
return null;
}
$this->setupPrivKey();
$encrypted = base64_decode($encrypted);
$result = openssl_private_decrypt($encrypted, $decrypted, $this->_privKey);
if ($result) {
return $decrypted;
}
return null;
}
/**
* 公钥加密
*
*/
public function pubEncrypt($data)
{
if (!is_string($data)) {
return null;
}
$this->setupPubKey();
$result = openssl_public_encrypt($data, $encrypted, $this->_pubKey);
if ($result) {
return base64_encode($encrypted);
}
return null;
}
/**
* 公钥解密
*
*/
public function pubDecrypt($crypted)
{
if (!is_string($crypted)) {
return null;
}
$this->setupPubKey();
$crypted = base64_decode($crypted);
$result = openssl_public_decrypt($crypted, $decrypted, $this->_pubKey);
if ($result) {
return $decrypted;
}
return null;
}
/**
* __destruct
*
*/
public function __destruct() {
@fclose($this->_privKey);
@fclose($this->_pubKey);
}
}
?>
测试
$rsa = new Rsa('ssl-key');
//私钥加密,公钥解密
echo "待加密数据:segmentfault.com\n";
$pre = $rsa->privEncrypt("segmentfault.com");
echo "加密后的密文:\n" . $pre . "\n";
$pud = $rsa->pubDecrypt($pre);
echo "解密后数据:" . $pud . "\n";
//公钥加密,私钥解密
echo "待加密数据:segmentfault.com\n";
$pue = $rsa->pubEncrypt("segmentfault.com");
echo "加密后的密文:\n" . $pue . "\n";
$prd = $rsa->privDecrypt($pue);
echo "解密后数据:" . $prd;
php实现非对称加密的更多相关文章
- RSA非对称加密,使用OpenSSL生成证书,iOS加密,java解密
最近换了一份工作,工作了大概一个多月了吧.差不多得有两个月没有更新博客了吧.在新公司自己写了一个iOS的比较通用的可以架构一个中型应用的不算是框架的一个结构,并已经投入使用.哈哈 说说文章标题的相关的 ...
- 个人理解c#对称加密 非对称加密 散列算法的应用场景
c#类库默认实现了一系列加密算法在System.Security.Cryptography; 命名空间下 对称加密 通过同一密匙进行加密和解密.往往应用在内部数据传输情况下.比如公司a程序 和B程序 ...
- 介绍DSA数字签名,非对称加密的另一种实现
接下来我们介绍DSA数字签名,非对称加密的另一种实现. DSA DSA-Digital Signature Algorithm 是Schnorr和ElGamal签名算法的变种,被美国NIST作为DSS ...
- 非对称加密RSA的应用及在C#中的实现
quote: http://www.cnblogs.com/happinessCodes/archive/2010/07/27/1786404.html 一说到数据的加密,常常会涉及到这几个单词: ...
- Atitit RSA非对称加密原理与解决方案
Atitit RSA非对称加密原理与解决方案 1.1. 一.一点历史 1 1.2. 八.加密和解密 2 1.3. 二.基于RSA的消息传递机制 3 1.4. 基于rsa的授权验证机器码 4 1.5. ...
- 和安全有关的那些事(非对称加密、数字摘要、数字签名、数字证书、SSL、HTTPS及其他)
转自http://blog.csdn.net/bluishglc/article/details/7585965 对于一般的开发人员来说,很少需要对安全领域内的基础技术进行深入的研究,但是鉴于日常系统 ...
- ssl原理,非对称加密握手,对称加密传输
SSL的基本思想是用非对称加密来建立链接(握手阶段),用对称加密来传输数据(传输阶段).这样既保证了密钥分发的安全,也保证了通信的效率. SSL握手,单方服务器认证(一般的浏览器上网) SSL握手,双 ...
- OpenSSL - RSA非对称加密实现
非对称加密:即两端使用一对不同的密钥进行加密. 在非对称加密中,需要两对密钥,公钥和私钥. 公钥个私钥属于对立关系,一把加密后,只有另一把才可以进行解密. 公钥数据加密 数字证书内包含了公钥,在进行会 ...
- php使用openssl来实现RSA(非对称加密)
使用非对称加密主要是借助openssl的公钥和私钥,用公钥加密私钥解密,或者私钥加密公钥解密. 1.安装openssl和PHP的openssl扩展 2.生成私钥:openssl genrsa 用于生成 ...
- RSA非对称加密 php的openssl实现
<?php /** * 使用openssl实现非对称加密 * @since 2010-07-08 */ class Rsa { /** * private key */ private $_pr ...
随机推荐
- ES6-babel转码
关于BaBel转码 有人问我babel的功能以及执行的过程和配置,在网上查阅了大量的资料~收集到这些~有错请指出,及时修改. ------------------------------------- ...
- vue轮播插件vue-awesome-swiper
https://surmon-china.github.io/vue-awesome-swiper/ 第一步安装 npm install vue-awesome-swiper --save 第二部在m ...
- [Codeforces 872]比赛记录
强行打了$cf$上的第一场比赛,现在感觉自己的$rating$会炸飞= = A 这是练习输入输出吗QAQ,竟然$WA$了两遍QAQ,我$WA$的一声就哭了出来啊QAQ B 好像很水的乱扫就好了,m ...
- 用c++编程:用两个栈实现队列
栈s1和栈s2,栈s1专门为入队,栈s2专门为出队. 入队: 当s1和s2都为空时,直接入队s1. 当s1为空,s2不为空时,把s2的元素都倒回s1,然后再入队s1 出队: 当s2不为空时,直接出队s ...
- Linux(1):fork函数
ps:每一篇博客不过为了记录学习的过程,并反思总结,如有错误,还望指正. 函数原型:extern __pid_t fork (void) __THROWNL; 该函数包括于头文件unistd.h中. ...
- java.text.ParseException: Unparseable date: "2015-06-09 hh:56:19"
1.错误描写叙述 [DEBUG:]2015-06-09 16:56:19,520 [-------------------transcation start!--------------] java. ...
- lzugis——Arcgis Server for JavaScript API之自己定义InfoWindow
用过Arcgis Server for JavaScript API肯定知道InfoWIndow.你在用InfoWindow的时候会发现各种问题,比如不能全然显示的问题,遮盖对象的问题等等.所以呢我在 ...
- RPC通信功能实现
Table of Contents RPC通信功能实现 配置參数 调用方法 RPC通信功能实现 HBase的RPC通信功能主要基于Protobuf和NIO这两个组件来实现.在通信管道上选择的是prot ...
- 605B. Lazy Student(codeforces Round 335)
B. Lazy Student time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- 使用excel进行数据挖掘(4)---- 突出显示异常值
使用excel进行数据挖掘(4)---- 突出显示异常值 在配置环境后.能够使用excel进行数据挖掘. 环境配置问题可參阅: http://blog.csdn.net/xinxing__8185/a ...