您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 通化分类信息网,免费分类信息发布

使用openssl实现rsa非对称加密算法示例_PHP教程

2024/3/25 21:48:40发布7次查看
这篇文章主要介绍了使用openssl实现rsa非对称加密算法的示例,大家参考使用吧
 代码如下:_keypath = $path;        }         /**         * create the key pair,save the key to $this->_keypath         */        public function 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);        }         /**         * setup the private key         */        public function setupprivkey()        {                if(is_resource($this->_privkey)){                        return true;                }                $file = $this->_keypath . directory_separator . 'priv.key';                $prk = file_get_contents($file);                $this->_privkey = openssl_pkey_get_private($prk);                return true;        }         /**         * setup the public key         */        public function setuppubkey()        {                if(is_resource($this->_pubkey)){                        return true;                }                $file = $this->_keypath . directory_separator .  'pub.key';                $puk = file_get_contents($file);                $this->_pubkey = openssl_pkey_get_public($puk);                return true;        }         /**         * encrypt with the private key         */        public function privencrypt($data)        {                if(!is_string($data)){                        return null;                }                 $this->setupprivkey();                 $r = openssl_private_encrypt($data, $encrypted, $this->_privkey);                if($r){                        return base64_encode($encrypted);                }                return null;        }         /**         * decrypt with the private key         */        public function privdecrypt($encrypted)        {                if(!is_string($encrypted)){                        return null;                }                 $this->setupprivkey();                 $encrypted = base64_decode($encrypted);                 $r = openssl_private_decrypt($encrypted, $decrypted, $this->_privkey);                if($r){                        return $decrypted;                }                return null;        }         /**         * encrypt with public key         */        public function pubencrypt($data)        {                if(!is_string($data)){                        return null;                }                 $this->setuppubkey();                 $r = openssl_public_encrypt($data, $encrypted, $this->_pubkey);                if($r){                        return base64_encode($encrypted);                }                return null;        }         /**         * 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 
http://www.bkjia.com/phpjc/727549.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/727549.htmltecharticle这篇文章主要介绍了使用openssl实现rsa非对称加密算法的示例,大家参考使用吧 代码如下:?php/*** 使用openssl实现非对称加密* @since 2010-07-08*...
通化分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录