• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

使用openssl实现rsa非对称加密算法示例_php实例

php 搞代码 3年前 (2022-01-25) 11次浏览 已收录 0个评论
<?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


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:使用openssl实现rsa非对称加密算法示例_php实例

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址