<?php<BR>/**<BR> * 使用openssl实现非对称加密<BR> * @since 2010-07-08<BR> */<BR>class Rsa<BR>{<BR> /**<BR> * private key<BR> */<BR> private $_privKey;<br><br> /**<BR> * public key<BR> */<BR> private $_pubKey;<br><br> /**<BR> * the keys saving path<BR> */<BR> private $_keyPath;<br><br> /**<BR> * the construtor,the param $path is the keys saving path<BR> */<BR> public function __construct($path)<BR> {<BR> if(empty($path) || !is_dir($path)){<BR> throw new Exception('Must set the keys save path');<BR> }<br><br> $this->_keyPath = $path;<BR> }<br><br> /**<BR> * create the key pair,save the key to $this->_keyPath<BR> */<BR> public function createKey()<BR> {<BR> $r = openssl_pkey_new();<BR> openssl_pkey_export($r, $privKey);<BR> file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR . 'priv.key', $privKey);<BR> $this->_privKey = openssl_pkey_get_public($privKey);<br><br> $rp = openssl_pkey_get_details($r);<BR> $pubKey = $rp['key'];<BR> file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR . 'pub.key', $pubKey);<BR> $this->_pubKey = openssl_pkey_get_public($pubKey);<BR> }<br><br> /**<BR> * setup the private key<BR> */<BR> public function setupPrivKey()<BR> {<BR> if(is_resource($this->_privKey)){<BR> return true;<BR> }<BR> $file = $this->_keyPath . DIRECTORY_SEPARATOR . 'priv.key';<BR> $prk = file_get_contents($file);<BR> $this->_privKey = openssl_pkey_get_private($prk);<BR> return true;<BR> }<br><br> /**<BR> * setup the public key<BR> */<BR> public function setupPubKey()<BR> {<BR> if(is_resource($this->_pubKey)){<BR> return true;<BR> }<BR> $file = $this->_keyPath . DIRECTORY_SEPARATOR . 'pub.key';<BR> $puk = file_get_contents($file);<BR> $this->_pubKey = openssl_pkey_get_public($puk);<BR> return true;<BR> }<br><br> /**<BR> * encrypt with the private key<BR> */<BR> public function privEncrypt($data)<BR> {<BR> if(!is_string($data)){<BR> return null;<BR> }<br><br> $this->setupPrivKey();<br><br> $r = openssl_private_encrypt($data, $encrypted, $this->_privKey);<BR> if($r){<BR> return base64_encode($encrypted);<BR> }<BR> return null;<BR> }<br><br> /**<BR> * decrypt with the private key<BR> */<BR> public function privDecrypt($encrypted)<BR> {<BR> if(!is_string($encrypted)){<BR> return null;<BR> }<br><br> $this->setupPrivKey();<br><br> $encrypted = base64_decode($encrypted);<br><br> $r = openssl_private_decrypt($encrypted, $decrypted, $this->_privKey);<BR> if($r){<BR> return $decrypted;<BR> }<BR> return null;<BR> }<br><br> /**<BR> * encrypt with public key<BR> */<BR> public function pubEncrypt($data)<BR> {<BR> if(!is_string($data)){<BR> return null;<BR> }<br><br> $this->setupPubKey();<br><br> $r = openssl_public_encrypt($data, $encrypted, $this->_pubKey);<BR> if($r){<BR> return base64_encode($encrypted);<BR> }<BR> return null;<BR> }<br><br> /**<BR> <p>5本文来源gao!daima.com搞$代!码#网#</p><pre>搞代gaodaima码
* 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:我是老鳖
‘;
$pre = $rsa->privEncrypt(‘我是老鳖’);
echo ‘private encrypted:
‘ . $pre . ‘
‘;
$pud = $rsa->pubDecrypt($pre);
echo ‘public decrypted:’ . $pud . ‘
‘;
//公钥加密,私钥解密
echo ‘source:干IT的
‘;
$pue = $rsa->pubEncrypt(‘干IT的’);
echo ‘public encrypt:
‘ . $pue . ‘
‘;
$prd = $rsa->privDecrypt($pue);
echo ‘private decrypt:’ . $prd;
?>
需要注意的是apache要支持OpenSSL