/** * public key */ pr " /> 欧美hdxxxx,日本阿v直播在线,国产精品一区二区四区

天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

使用openssl實(shí)現(xiàn)rsa非對(duì)稱加密算法示例

復(fù)制代碼 代碼如下:
<?php
/**
 * 使用openssl實(shí)現(xiàn)非對(duì)稱加密
 * @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);
        }

}

//以下是一個(gè)簡(jiǎn)單的測(cè)試demo,如果不需要請(qǐng)刪除
$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技術(shù)使用openssl實(shí)現(xiàn)rsa非對(duì)稱加密算法示例,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 芒果影院网站在线观看 | 97人妻AV天天澡夜夜爽 | 欧美久久综合性欧美 | 国产免费人成在线视频有码 | 亚洲 欧美 国产 综合 播放 | 99久久免费国内精品 | 一区二区三区内射美女毛片 | 久久亚洲午夜牛牛影视 | 超级碰碰青草久热国产 | a免费在线观看视频 | 国产精品婷婷五月久久久久 | 99国产精品久久 | 中文天堂www资源 | 碰超成人在线公开免费视频 | 污污内射久久一区二区欧美日韩 | 俄罗斯1819y0u | 蜜臀久久99精品久久久久久做爰 | 国产成人在线视频免费观看 | 亚洲伦理精品久久 | 久久人人爽人人片AV人成 | 97色伦久久视频在观看 | 久久这里只精品国产99re66 | 亲胸揉胸膜下刺激视频在线观看 | 国产盗摄一区二区三区 | 国产免费毛片在线观看 | 国产精品第100页 | 偷拍国产精品在线播放 | 99精品国产免费观看视频 | 中字幕视频在线永久在线观看免费 | 91精品婷婷国产综合久久8 | 97国产成人精品免费视频 | SM调教贱屁股眼哭叫求饶H | 国产精品亚洲二线在线播放 | 琪琪电影午夜理论片77网 | 精品欧美一区二区三区四区 | 久久久欧美国产精品人妻噜噜 | 四虎免费影院 | 免费果冻传媒在线完整观看 | 青苹果乐园在线观看电视剧 | 国产精品色吧国产精品 | 国产最猛性XXXX69交 |