久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長(zhǎng)資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      php實(shí)現(xiàn)不對(duì)稱(chēng)加密的方法

      php實(shí)現(xiàn)不對(duì)稱(chēng)加密的方法:首先創(chuàng)建一個(gè)PHP示例文件;然后使用openssl實(shí)現(xiàn)非對(duì)稱(chēng)加密;最后通過(guò)“$rsa = new Rsa('ssl-key');”進(jìn)行測(cè)試即可。

      php實(shí)現(xiàn)不對(duì)稱(chēng)加密的方法

      推薦:《PHP視頻教程》

      PHP實(shí)現(xiàn)非對(duì)稱(chēng)加密


      至于什么是非對(duì)稱(chēng)加密,這里就不說(shuō)啦,大家谷歌去吧。這里說(shuō)明的是,最近在做一個(gè)對(duì)外的充值加密服務(wù),那么涉及到這個(gè)加密的處理,中間遇到幾個(gè)小問(wèn)題,所以記錄下,方便自己下次查閱。

      詳細(xì)代碼

      <?php /**  * 使用openssl實(shí)現(xiàn)非對(duì)稱(chēng)加密  *   * @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('請(qǐng)指定密鑰文件地址目錄');         }         $this->_keyPath = $path;     }     /**      * 創(chuàng)建公鑰和私鑰      *       */     public function createKey()     {         $config = [             "config" => 'D:wampbinapacheapache2.4.9confopenssl.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);     }     /**      * 設(shè)置私鑰      *       */     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;     }     /**      * 設(shè)置公鑰      *       */     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);     } } ?>

      測(cè)試

      $rsa = new Rsa('ssl-key'); //私鑰加密,公鑰解密 echo "待加密數(shù)據(jù):segmentfault.comn"; $pre = $rsa->privEncrypt("segmentfault.com"); echo "加密后的密文:n" . $pre . "n"; $pud = $rsa->pubDecrypt($pre); echo "解密后數(shù)據(jù):" . $pud . "n"; //公鑰加密,私鑰解密 echo "待加密數(shù)據(jù):segmentfault.comn"; $pue = $rsa->pubEncrypt("segmentfault.com"); echo "加密后的密文:n" . $pue . "n"; $prd = $rsa->privDecrypt($pue); echo "解密后數(shù)據(jù):" . $prd;

      重要問(wèn)題

      這里特別要注意的是在配置中要指定openssl.cnf的文件地址,或者設(shè)置個(gè)OPENSSL_CONF全局變量就可以了。

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)