激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

香港云服务器
服務器之家 - 編程語言 - PHP教程 - PHP封裝的非對稱加密RSA算法示例

PHP封裝的非對稱加密RSA算法示例

2019-10-04 20:26墨子哲 PHP教程

這篇文章主要介紹了PHP封裝的非對稱加密RSA算法,結合實例形式分析了php實現的RSA加密算法類及其相關使用技巧,需要的朋友可以參考下

本文實例講述了PHP封裝的非對稱加密RSA算法。分享給大家供大家參考,具體如下:

將php的openssl擴展中的非對稱加密函數封裝成一個Rsa類。

需要注意的是,在windows上,需要打開openssl的配置文件,請參照官方的openssl擴展安裝文檔。

在windows上安裝openssl擴展

1、將php路徑下的兩個庫文件libeay32.dll和ssleay32.dll復制到操作system32下

2、配置openssl配置文件的位置,在php的路徑下,有文件extras/openssl/openssl.cnf,添加環境變量OPENSSL_CONF指向這個文件的全路徑。如何添加環境變量請google搜索之。

3、在php.ini里添加一行extension=php_openssl.dll

使用的demo:

01//====================demo=======================
02//以下是一個簡單的測試demo,如果不需要請刪除
03$rsa = new Rsa('sslkey'); //sslkey為存放密鑰的路徑,將已有的密鑰文件復制到該路徑下,公鑰名稱為pub.key,私鑰名稱為priv.key
04$rsa->createKey(); //創建一對密鑰,如果密鑰對已經存在,不需調用
05//私鑰加密,公鑰解密
06echo 'source:服務器之家<br />';
07$pre = $rsa->privEncrypt('服務器之家');
08echo 'private encrypted:<br />' . $pre . '<br />';
09$pud = $rsa->pubDecrypt($pre);
10echo 'public decrypted:' . $pud . '<br />';
11//公鑰加密,私鑰解密
12echo 'source:干IT的<br />';
13$pue = $rsa->pubEncrypt('干IT的');
14echo 'public encrypt:<br />' . $pue . '<br />';
15$prd = $rsa->privDecrypt($pue);
16echo 'private decrypt:' . $prd;
17//========================demo======================

本示例在windows7、php 5.2.14、openssl 0.98下開發

001<?php
002/**
003 * 使用openssl實現非對稱加密
004 *
005 */
006class Rsa
007{
008  /**
009   * private key
010   */
011    private $_privKey;
012    /**
013     * public key
014     */
015    private $_pubKey;
016    /**
017     * the keys saving path
018     */
019    private $_keyPath;
020    /**
021     * the construtor,the param $path is the keys saving path
022     */
023    public function __construct($path)
024    {
025        if(empty($path) || !is_dir($path)){
026            throw new Exception('Must set the keys save path');
027        }
028        $this->_keyPath = $path;
029    }
030    /**
031     * create the key pair,save the key to $this->_keyPath
032     */
033    public function createKey()
034    {
035        $r = openssl_pkey_new();
036        openssl_pkey_export($r, $privKey);
037        file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR . 'priv.key', $privKey);
038        $this->_privKey = openssl_pkey_get_private($privKey);
039        $rp = openssl_pkey_get_details($r);
040        $pubKey = $rp['key'];
041        file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR . 'pub.key', $pubKey);
042        $this->_pubKey = openssl_pkey_get_public($pubKey);
043    }
044    /**
045     * setup the private key
046     */
047    public function setupPrivKey()
048    {
049        if(is_resource($this->_privKey)){
050            return true;
051        }
052        $file = $this->_keyPath . DIRECTORY_SEPARATOR . 'priv.key';
053        $prk = file_get_contents($file);
054        $this->_privKey = openssl_pkey_get_private($prk);
055        return true;
056    }
057    /**
058     * setup the public key
059     */
060    public function setupPubKey()
061    {
062        if(is_resource($this->_pubKey)){
063            return true;
064        }
065        $file = $this->_keyPath . DIRECTORY_SEPARATOR . 'pub.key';
066        $puk = file_get_contents($file);
067        $this->_pubKey = openssl_pkey_get_public($puk);
068        return true;
069    }
070    /**
071     * encrypt with the private key
072     */
073    public function privEncrypt($data)
074    {
075        if(!is_string($data)){
076            return null;
077        }
078        $this->setupPrivKey();
079        $r = openssl_private_encrypt($data, $encrypted, $this->_privKey);
080        if($r){
081            return base64_encode($encrypted);
082        }
083        return null;
084    }
085    /**
086     * decrypt with the private key
087     */
088    public function privDecrypt($encrypted)
089    {
090        if(!is_string($encrypted)){
091            return null;
092        }
093        $this->setupPrivKey();
094        $encrypted = base64_decode($encrypted);
095        $r = openssl_private_decrypt($encrypted, $decrypted, $this->_privKey);
096        if($r){
097            return $decrypted;
098        }
099        return null;
100    }
101    /**
102     * encrypt with public key
103     */
104    public function pubEncrypt($data)
105    {
106        if(!is_string($data)){
107            return null;
108        }
109        $this->setupPubKey();
110        $r = openssl_public_encrypt($data, $encrypted, $this->_pubKey);
111        if($r){
112            return base64_encode($encrypted);
113        }
114        return null;
115    }
116    /**
117     * decrypt with the public key
118     */
119    public function pubDecrypt($crypted)
120    {
121        if(!is_string($crypted)){
122            return null;
123        }
124        $this->setupPubKey();
125        $crypted = base64_decode($crypted);
126        $r = openssl_public_decrypt($crypted, $decrypted, $this->_pubKey);
127        if($r){
128            return $decrypted;
129        }
130        return null;
131    }
132    public function __destruct()
133    {
134        @ fclose($this->_privKey);
135        @ fclose($this->_pubKey);
136    }
137}

希望本文所述對大家PHP程序設計有所幫助。

延伸 · 閱讀

精彩推薦
316
主站蜘蛛池模板: 亚洲人成网站在e线播放 | 9999在线视频 | 久久精品一区二区三区四区五区 | 久久久久久久久国产精品 | 一级免费黄色免费片 | 日本在线看 | 色蜜桃av| 日本黄色免费片 | 国产精品视频二区不卡 | 欧洲精品久久久久69精品 | 激情久久精品 | 31freehdxxxx欧美 | 欧美一区二区三区久久 | 1000部精品久久久久久久久 | 亚洲xxx在线观看 | 久草在线资源视频 | 久久我不卡 | 国产日产精品久久久久快鸭 | 狠狠操视频网站 | 欧美日韩中文字幕在线 | 久成人 | 国产噜噜噜 | 久久久久久久.comav | 日韩在线播放第一页 | 国产免费网站视频 | 色悠悠久久久久 | 91看片淫黄大片欧美看国产片 | 一级黄色淫片 | 成人国产精品齐天大性 | 免费a级毛片永久免费 | 一区二区三区日韩电影 | 久久亚洲精品久久国产一区二区 | 看国产毛片 | 日本aaaa片毛片免费观看视频 | 一级做a爱片性色毛片 | 亚洲性生活免费视频 | 色污视频在线观看 | 精品久久久久久久久久中出 | 国产一区二区三区在线免费观看 | 国产精品jk白丝蜜臀av软件 | 国产精品久久久久久久久久久久久久久久 |