美文网首页
Rsa 加密

Rsa 加密

作者: 安晓生 | 来源:发表于2020-03-30 13:30 被阅读0次

这是一个原生类,其实现在框架都有自带的插件了。
大家可以根据框架,去搜索框架带的composer插件类库。

class Rsa
{
    private $_config = [
        'public_key' => '',
        'private_key' => '',
    ];

    public function __construct($private_key_filepath, $public_key_filepath) {
        $this->_config['private_key'] = $this->_getContents($private_key_filepath);
        $this->_config['public_key'] = $this->_getContents($public_key_filepath);
    }

    /**
     * @uses 获取文件内容
     * @param $file_path string
     * @return bool|string
     */
    private function _getContents($file_path) {
        file_exists($file_path) or die ('密钥或公钥的文件路径错误');
        return file_get_contents($file_path);
    }

    /**     
     * @uses 获取私钥
     * @return bool|resource     
     */ 
    private function _getPrivateKey() {
       $priv_key = $this->_config['private_key'];
       return openssl_pkey_get_private($priv_key);
    }

    /**     
     * @uses 获取公钥
     * @return bool|resource     
     */    
    private function _getPublicKey() {        
        $public_key = $this->_config['public_key'];
        return openssl_pkey_get_public($public_key);
    }

    /**     
     * @uses 私钥加密
     * @param string $data     
     * @return null|string     
     */    
    public function privEncrypt($data = '') {        
        if (!is_string($data)) {
            return null;       
        }
        return openssl_private_encrypt($data, $encrypted, $this->_getPrivateKey()) ? base64_encode($encrypted) : null;
    }

    /**     
     * @uses 公钥加密     
     * @param string $data     
     * @return null|string     
     */    
    public function publicEncrypt($data = '') {        
        if (!is_string($data)) {
            return null;        
        }        
        return openssl_public_encrypt($data, $encrypted, $this->_getPublicKey()) ? base64_encode($encrypted) : null;
    }

    /**     
     * @uses 私钥解密     
     * @param string $encrypted     
     * @return null     
     */    
    public function privDecrypt($encrypted = '') {        
        if (!is_string($encrypted)) {
            return null;        
        }
        return (openssl_private_decrypt(base64_decode($encrypted), $decrypted, $this->_getPrivateKey())) ? $decrypted : null;
    }    

    /**     
     * @uses 公钥解密     
     * @param string $encrypted     
     * @return null     
     */    
    public function publicDecrypt($encrypted = '') {        
        if (!is_string($encrypted)) {
            return null;        
        }        
           return (openssl_public_decrypt(base64_decode($encrypted), $decrypted, $this->_getPublicKey())) ? $decrypted : null;
    }
}


调用rsa类
$private_key = 'private_key.pem'; // 私钥路径
$public_key = 'rsa_public_key.pem'; // 公钥路径
$rsa = new Rsa($private_key, $public_key);

$origin_data = '这是一条测试数据';

$ecryption_data = $rsa->privEncrypt($origin_data);

$decryption_data = $rsa->publicDecrypt($ecryption_data);

echo '私钥加密后的数据为:' . $ecryption_data;
echo PHP_EOL;
echo '公钥解密后的数据为: ' . $decryption_data;
echo PHP_EOL;

相关文章

  • RSA加密方式

    RSA加密方式 获取RSA密钥 加密 解密 js库

  • C# RSA加解密和MD5加密

    1.RSA加密 2.RSA解密 3.RSA签名 RSA签名验签 4.MD5加密

  • RSA签名认证

    RSA可汗学院第一章 RSA加密 RSA加密原理第一章 RSA加密原理第二章 如何生成RSA公钥私钥 生成类似支付...

  • kotlin版本RSA非对称加密解密与分段加密解密

    基于kotlin语言的RSA非对称加密解密与分段加密解密 RSA非对称加密 RSA非对称加密的具体算法与来源我就不...

  • # RSA 公钥加密算法

    # RSA 公钥加密算法 # RSA 公钥加密算法

  • 命令

    文件编译 加密解密 1. 对称加密 DES AES Base64 2. 非对称加密 RSA RSA加密:公钥加密,...

  • Java加密

    MD5加密: RSA加密: CBC加密:

  • RSA加密算法详解

    什么是RSA算法? RSA加密算法是一种非对称加密算法。在公开密钥加密和电子商业中RSA被广泛使用。RSA是197...

  • 项目加密 RSA+DES3加密方式

    rsa加密参考网址 rsa加密参考网址 des3加密支持中文加密 des加密参考网址 加密的方式为 先将数据进行D...

  • 常用的加密

    加密 RSA MD5 SHA-1 DES 3DES RSA RSA是一种非对称加密算法(公钥加密,私钥解密)。对极...

网友评论

      本文标题:Rsa 加密

      本文链接:https://www.haomeiwen.com/subject/affvuhtx.html