您好,欢迎访问一九零五行业门户网

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

复制代码 代码如下:
_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
其它类似信息

推荐信息