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 ...
随机推荐
- 7.1.2 Python 内置异常类层次结构
这一节就是拿来主义了,连接:https://blog.csdn.net/Karen_Yu_/article/details/78629918 异常名称 描述 BaseException 所有异常的基类 ...
- Spring整合Junit框架进行单元测试Demo
一.开发环境 eclipse版本:4.6.1 maven版本:3.3.3 junit版本:4.12 spring版本:4.1.5.RELEASE JDK版本:1.8.0_111 二.项目结构 图 三. ...
- hdu2004 成绩转换【C++】
成绩转换 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- GeoTrust 企业(OV)型 增强版(EV) SSL证书
GeoTrust 企业(OV)型 增强版(EV) SSL证书(GeoTrust True BusinessID with EV SSL Certificates),验证域名所有权,更严格的验证企业 ...
- 【Codeforces Global Round 1 E】Magic Stones
[链接] 我是链接,点我呀:) [题意] 你可以把c[i]改成c[i+1]+c[i-1]-c[i] (2<=i<=n-1) 问你能不能把每一个c[i]都换成对应的t[i]; [题解] d[ ...
- redis学习五,redis集群搭建及添加主从节点
redis集群 java架构师项目实战,高并发集群分布式,大数据高可用,视频教程 在redis3.0之前,出现了sentinel工具来监控各个Master的状态(可以看上一篇博客).如果Master异 ...
- Python 5 运算符
数学运算符: + 加 1 + 1 = 2 - 减 2 - 1 = 1 × 乘 2 × 2 = 4 / 除 3 / 2 = 1.5 不同版本可能显示小数位数不同 ...
- noip模拟赛 解谜游戏
题目描述LYK进了一家古董店,它很想买其中的一幅画.但它带的钱不够买这幅画.幸运的是,老板正在研究一个问题,他表示如果LYK能帮他解出这个问题的话,就把这幅画送给它.老板有一个n*m的矩阵,他想找一个 ...
- 利用fontforge制作自己的字体
最近手伤了,写代码特别慢,索性就干干一些奇奇怪怪的事情. 发现我电脑上的中文字体很是奇怪,于是便去找了中英混合的等宽字体. 满足条件的只找到了YaHei Consolas Hybrid,是微软的Con ...
- Configuration must specify a spooling directory
启动spooling源时报错: 原因:spooling配置文件有误 a1.sources.r1.type = spooldir a1.sources.r1.spooldir = /usr/local/ ...