<?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实现非对称加密的更多相关文章

  1. RSA非对称加密,使用OpenSSL生成证书,iOS加密,java解密

    最近换了一份工作,工作了大概一个多月了吧.差不多得有两个月没有更新博客了吧.在新公司自己写了一个iOS的比较通用的可以架构一个中型应用的不算是框架的一个结构,并已经投入使用.哈哈 说说文章标题的相关的 ...

  2. 个人理解c#对称加密 非对称加密 散列算法的应用场景

    c#类库默认实现了一系列加密算法在System.Security.Cryptography; 命名空间下 对称加密 通过同一密匙进行加密和解密.往往应用在内部数据传输情况下.比如公司a程序 和B程序 ...

  3. 介绍DSA数字签名,非对称加密的另一种实现

    接下来我们介绍DSA数字签名,非对称加密的另一种实现. DSA DSA-Digital Signature Algorithm 是Schnorr和ElGamal签名算法的变种,被美国NIST作为DSS ...

  4. 非对称加密RSA的应用及在C#中的实现

    quote: http://www.cnblogs.com/happinessCodes/archive/2010/07/27/1786404.html   一说到数据的加密,常常会涉及到这几个单词: ...

  5. Atitit RSA非对称加密原理与解决方案

    Atitit RSA非对称加密原理与解决方案 1.1. 一.一点历史 1 1.2. 八.加密和解密 2 1.3. 二.基于RSA的消息传递机制  3 1.4. 基于rsa的授权验证机器码 4 1.5. ...

  6. 和安全有关的那些事(非对称加密、数字摘要、数字签名、数字证书、SSL、HTTPS及其他)

    转自http://blog.csdn.net/bluishglc/article/details/7585965 对于一般的开发人员来说,很少需要对安全领域内的基础技术进行深入的研究,但是鉴于日常系统 ...

  7. ssl原理,非对称加密握手,对称加密传输

    SSL的基本思想是用非对称加密来建立链接(握手阶段),用对称加密来传输数据(传输阶段).这样既保证了密钥分发的安全,也保证了通信的效率. SSL握手,单方服务器认证(一般的浏览器上网) SSL握手,双 ...

  8. OpenSSL - RSA非对称加密实现

    非对称加密:即两端使用一对不同的密钥进行加密. 在非对称加密中,需要两对密钥,公钥和私钥. 公钥个私钥属于对立关系,一把加密后,只有另一把才可以进行解密. 公钥数据加密 数字证书内包含了公钥,在进行会 ...

  9. php使用openssl来实现RSA(非对称加密)

    使用非对称加密主要是借助openssl的公钥和私钥,用公钥加密私钥解密,或者私钥加密公钥解密. 1.安装openssl和PHP的openssl扩展 2.生成私钥:openssl genrsa 用于生成 ...

  10. RSA非对称加密 php的openssl实现

    <?php /** * 使用openssl实现非对称加密 * @since 2010-07-08 */ class Rsa { /** * private key */ private $_pr ...

随机推荐

  1. [luoguP1197] [JSOI2008]星球大战(并查集)

    传送门 思维!重要的是思维! 题目让删边,然而并查集不好删边(并!查!集!啊) 我们离线处理,从后往前添边,这样并查集就可以用了. 用并查集维护连通块个数即可. ——代码 #include <c ...

  2. Solr插件的弊端

    在前文<Solr Update插件自定义条件索引>中,我介绍了如何通过插件的模式,自定义Solr的Update过程.但是在大半年的使用过程中,发现这种方式存在如下弊端. 1.环境难以维护. ...

  3. HTMLParser in python

    You can know form the name that the HTMLParser is something used to parse HTML files.  In python, th ...

  4. HDU 5226

    公式啊,公式啊....TAT 杭电题解.....高中生...... 对于卢卡斯定理,由于p较大,所以不可能按一般的来算,n,m较小,循处理出n!的逆元对p的,然后可以按照卢卡斯定理,降低,对后面的就可 ...

  5. Apache模块开发指南-APR池

    转:原文: http://blog.csdn.net/zmxiangde_88/article/details/8038150 ------------------------------------ ...

  6. 让devstack中的vm訪问外网

    devstack默认会建立一个Public网络,地址为172.24.4.0/24,可是这个网络并非运营商分配给我们的网络.所以仅仅能通过nat的方式让devstack建立的虚拟机訪问外网. br-ex ...

  7. Android之——AsyncTask和Handler对照

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46952835 AsyncTask和Handler对照 1 ) AsyncTask实 ...

  8. c++中cin的基本使用方法

    一.最主要的使用方法cin>> 接收一个数字.字符.字符串.遇"空格"."TAB"."回车"都结束 比如: <span s ...

  9. zoj1940

    链接:点击打开链接 题意:三维搜索'S'为起点,'E'为终点,求走出的最短时间 代码: #include <iostream> #include <stdio.h> #incl ...

  10. 欣喜若狂!今天最终成功把音频导入到iphone了,大半年的努力,靠的毅力和方法

    研究IOS 的助手也有大半年时间了,一直没有实现导入音视频文件的功能,主要是过程太复杂,而且基本上没有资料能够查询.经过不懈的努力,今天最终成功导入了一个mp3 文件到ipod,一切功能正常,期间经历 ...