openssl_PHP チュートリアルを使用した RSA 非対称暗号化アルゴリズムの実装例

WBOY
リリース: 2016-07-13 10:40:27
オリジナル
992 人が閲覧しました

この記事では、opensslを使用してrsa非対称暗号化アルゴリズムを実装する例を中心に紹介しますので、ぜひ参考にしてください

 代コード如下: _keyPath = $path;         }           /**         * キーペアを作成し、キーを $this->_keyPath に保存します          */         パブリック関数 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);         }           /**         * 秘密キーを設定します          */         パブリック関数 setupPrivKey()         {                 if(is_resource($this->_privKey)){                         true を返します。                 }                 $file = $this->_keyPath 。 DIRECTORY_SEPARATOR 。 '秘密キー';                 $prk = file_get_contents($file);                 $this->_privKey = openssl_pkey_get_private($prk);                 true を返します。         }           /**         * 公開キーを設定します          */         パブリック関数 setupPubKey()         {                 if(is_resource($this->_pubKey)){                         true を返します。                 }                 $file = $this->_keyPath 。 DIRECTORY_SEPARATOR 。  'pub.key';                 $puk = file_get_contents($file);                 $this->_pubKey = openssl_pkey_get_public($puk);                 true を返します。         }           /**         * 秘密鍵で暗号化する          */         パブリック関数 privEncrypt($data)         {                 if(!is_string($data)){                         null を返します。                 }                  $this->setupPrivKey();                   $r = openssl_private_encrypt($data, $encrypted, $this->_privKey);                 if($r){                         Base64_encode($encrypted) を返します。                 }                 null を返します。         }           /**         * 秘密鍵を使用して復号化します          */         パブリック関数 privDecrypt($encrypted)         {                 if(!is_string($encrypted)){                         null を返します。                 }                   $this->setupPrivKey();                   $encrypted =base64_decode($encrypted);                   $r = openssl_private_decrypt($encrypted, $decrypted, $this->_privKey);                 if($r){                         $decrypted を返します。                 }                 null を返します。         }           /**         * 公開鍵で暗号化する          */         パブリック関数 pubEncrypt($data)         {                 if(!is_string($data)){                         null を返します。                 }                   $this->setupPubKey();                   $r = openssl_public_encrypt($data, $encrypted, $this->_pubKey);                 if($r){                         Base64_encode($encrypted) を返します。                 }                 null を返します。         }           /**         * 公開キーを使用して復号化します          */         パブリック関数 pubDecrypt($crypted)         {                 if(!is_string($crypted)){                         null を返します。                 }                   $this->setupPubKey();                   $crypted = Base64_decode($crypted);                   $r = openssl_public_decrypt($crypted, $decrypted, $this->_pubKey);                 if($r){                         $decrypted を返します。                 }                 null を返します。         }           パブリック関数 __destruct()         {                 @ fclose($this->_privKey);                 @ fclose($this->_pubKey);         }   }   //以下は非常に簡単なデモです。必要がない場合は削除してください $rsa = 新しい Rsa('ssl-key');   //私钥加密、公钥解密 echo 'ソース:我是老鳖
'; $pre = $rsa->privEncrypt('我是老鳖'); echo 'プライベート暗号化:
' 。 $pre 。 '
';   $pud = $rsa->pubDecrypt($pre); echo 'パブリック復号化:' 。 $pud 。 '
';   //公钥加密、私钥解密 echo 'source:干ITの
'; $pue = $rsa->pubEncrypt('干IT的'); echo 'パブリック暗号化:
' 。 $pue 。 '
';   $prd = $rsa->privDecrypt($pue); echo 'プライベート復号化:' 。 $prd; ?>     注意が必要なのはapacheがOpenSSLをサポートしていることです  

www.bkjia.com本当http://www.bkjia.com/PHPjc/727549.html技術記事この記事では、主に openssl を使用して rsa 非対称暗号化アルゴリズムを実装する例を紹介します。次のコードを参照してください:?php/*** openssl を使用して非対称暗号化を実装する* @since 2010-07-08*...
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!