本文實例講述了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 | //私鑰加密,公鑰解密 |
06 | echo 'source:服務器之家<br />' ; |
07 | $pre = $rsa ->privEncrypt( '服務器之家' ); |
08 | echo 'private encrypted:<br />' . $pre . '<br />' ; |
09 | $pud = $rsa ->pubDecrypt( $pre ); |
10 | echo 'public decrypted:' . $pud . '<br />' ; |
11 | //公鑰加密,私鑰解密 |
12 | echo 'source:干IT的<br />' ; |
13 | $pue = $rsa ->pubEncrypt( '干IT的' ); |
14 | echo 'public encrypt:<br />' . $pue . '<br />' ; |
15 | $prd = $rsa ->privDecrypt( $pue ); |
16 | echo 'private decrypt:' . $prd ; |
17 | //========================demo====================== |
本示例在windows7、php 5.2.14、openssl 0.98下開發
001 | <?php |
002 | /** |
003 | * 使用openssl實現非對稱加密 |
004 | * |
005 | */ |
006 | class 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程序設計有所幫助。