/** * public key */ pr " /> 双性被疯狂灌满精NP,成人手机在线观看,亚洲乱码爆乳精品成人毛片

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

使用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技術使用openssl實現rsa非對稱加密算法示例,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 99热视频这里只有久久精品 | 色婷婷欧美在线播放内射 | 青青草原国产在线 | 芒果影院网站在线观看 | 影音先锋av男人资源 | 日本19禁啪啪吃奶大尺度 | 成人天堂婷婷青青视频在线观看 | 欧美另类极品videosbest | 国产成人精品男人的天堂网站 | 97人妻AV天天澡夜夜爽 | 亚洲国产区中文在线观看 | 久久精品黄色 | 99久久免热在线观看 | 久久精品中文字幕 | 精品视频在线观看视频免费视频 | 丰满老熟好大bbbxxx | 各种场合肉H校园1V1 | 超级乱淫片午夜电影网99 | 国产国拍亚洲精品av麻豆 | 国产精品一区二区三区免费 | 日日碰狠狠躁久久躁77777 | 无码精品AV久久久奶水 | 嘟嘟嘟WWW在线观看视频高清 | 亚洲视频国产 | 人妻熟女斩五十路0930 | 欧美Av无码高清在线 | 扒开小处30p| 国产亚洲国际精品福利 | 2020国产欧洲精品视频 | 97久久精品视频 | 暖暖的视频完整视频免费韩国 | 久久看片网 | 午夜伦理电影在线观免费 | 乐乐亚洲精品综合影院 | 欧美人与动牲交XXXXBBBB免费 | 国产AV视频二区在线观看 | FREECHINESE东北女人真爽 free18sex性自拍裸舞 | 久久精品国产99欧美精品亚洲 | 亚洲 欧美 国产 综合五月天 | 亚洲精品国产一区二区贰佰信息网 | 欧美一区二区在线观看 |