使用openssl实现rsa非对称加密算法示例
2015-01-24信息快讯网
这篇文章主要介绍了使用openssl实现rsa非对称加密算法的示例,大家参考使用吧
<?php
/**
* 使用openssl实现非对称加密
* @since 2010-07-08
*/
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,如果不需要请删除
$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;
?>
需要注意的是apache要支持OpenSSL
php去掉URL网址中带有PHPSESSID的配置方法
采用memcache在web集群中实现session的同步会话
windows7下安装php的php-ssh2扩展教程
PHP+Memcache实现wordpress访问总数统计(非插件)
ThinkPHP框架实现session跨域问题的解决方法
PHP使用GIFEncoder类处理gif图片实例
PHP使用GIFEncoder类生成gif动态滚动字幕
PHP使用GIFEncoder类生成的GIF动态图片验证码
ThinkPHP模板判断输出Present标签用法详解
编译PHP报错configure error Cannot find libmysqlclient under usr的解决方法
PHP 面向对象程序设计(oop)学习笔记(一) - 抽象类、对象接口、instanceof 和契约式编程
利用谷歌 Translate API制作自己的翻译脚本
ECMall支持SSL连接邮件服务器的配置方法详解
php开启openssl的方法
windows服务器中检测PHP SSL是否开启以及开启SSL的方法
PHP中isset()和unset()函数的用法小结
header导出Excel应用示例
测试php连接mysql是否成功的代码分享
php fsockopen解决办法 php实现多线程
用Zend Studio+PHPnow+Zend Debugger搭建PHP服务器调试环境步骤
PHP类继承 extends使用介绍
php环境下利用session防止页面重复刷新的具体实现
PHP修改session_id示例代码
php Calender(日历)代码分享
zend framework文件上传功能实例代码
PHP数据库链接类(PDO+Access)实例分享
wordpress自定义url参数实现路由功能的代码示例
PHP反射类ReflectionClass和ReflectionObject的使用方法
浅析PHP原理之变量(Variables inside PHP)
php调用Google translate_tts api实现代码
解析PHP中的unset究竟会不会释放内存
php修改NetBeans默认字体的大小
解析php php_openssl.dll的作用
浅析PKI加密解密 OpenSSL
php pki加密技术(openssl)详解