欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-30 09:20:41
复制代码 代码如下:

复制代码 代码如下:

<?php
/**
 * 使用openssl实现非对称加密
 * @since 2010-07-08
 */
class rsa
{
    /**
     * private key
     */
        private $_privkey;

        /**
         * public key
         */
        private $_pubkey;

        /**
         * the keys saving path
         */
        private $_keypath;

        /**
         * the construtor,the param $path is the keys saving path
         */
        public function __construct($path)
        {
                if(empty($path) || !is_dir($path)){
                        throw new exception('must set the keys save path');
                }

                $this->_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:我是老鳖<br />';
$pre = $rsa->privencrypt('我是老鳖');
echo 'private encrypted:<br />' . $pre . '<br />';

$pud = $rsa->pubdecrypt($pre);
echo 'public decrypted:' . $pud . '<br />';

//公钥加密,私钥解密
echo 'source:干it的<br />';
$pue = $rsa->pubencrypt('干it的');
echo 'public encrypt:<br />' . $pue . '<br />';

$prd = $rsa->privdecrypt($pue);
echo 'private decrypt:' . $prd;
?>


需要注意的是apache要支持openssl