php使用openssl来实现非对称加密

使用非对称加密主要是借助openssl的公钥和私钥,用公钥加密私钥解密,或者私钥加密公钥解密。

安装openssl和php的openssl扩展

生成私钥:openssl genrsa 用于生成rsa私钥文件,生成是可以指定私钥长度和密码保护

openssl genrsa -out rsa_private_key.pem 1024

生成公钥:rsa命令用于处理RSA密钥、格式转换和打印信息

openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem 

这里我们使用公钥加密,私钥解密

class Openssl
{

   /**
     * [$instance description]
     * @var null
     */
    protected static $instance = null;

   /**
     * [$error description]
     * @var null
     */
	public $error = null;

      /**
	 * [$rsa_private_key 移钥]
	 * @var string
	 */
	public $rsa_private_key = '';

       /**
	 * [$rsa_public_key 公钥]
	 * @var string
	 */
	public $rsa_public_key = '';

   /**
     * [__construct description]
     * @author: edge
     * @time: 2017/10/22
     */
    public function __construct()
    {
    	$this->rsa_private_key_path = __DIR__ . '/key/rsa_private_key.pem';
    	$this->rsa_public_key_path = __DIR__ . '/key/rsa_public_key.pem';
    	if (!file_exists($this->rsa_private_key_path)) {
    		throw new \Exception('私钥不存在');
    	}
    	if (!file_exists($this->rsa_public_key_path)) {
    		throw new \Exception('公钥不存在');
    	}
    	if(!extension_loaded('openssl')){
    		throw new \Exception('缺少openssl扩展');
    	}
    	$this->rsa_private_key = openssl_pkey_get_private(file_get_contents($this->rsa_private_key_path));
    	$this->rsa_public_key = openssl_pkey_get_public(file_get_contents($this->rsa_public_key_path));
    	if (!$this->rsa_private_key) {
    		throw new \Exception('私钥不可用');
    	}
    	if (!$this->rsa_public_key) {
    		throw new \Exception('公钥不可用');
    	}
    }

   /**
     * 单例
     * @author: edge
     * @time: 2017/10/22
     * @return class
     */
    public static function getInstance()
    {
        if(empty(static::$instance)){
            static::$instance = new static();
        }
        return static::$instance;
    }

    /**
     * [Base64Encrypt 公钥加密]
     * @author: edge
     * @time: 2017/10/22
     * @param string $str [description]
     */
    public function Base64Encrypt($str = '')
    {
    	if (!empty($str)) {
    		$str = json_encode($str);
    		if(openssl_public_encrypt($str,$sign,$this->rsa_public_key)){
    			return base64_encode($sign);
    		} else {
    			throw new \Exception('加密数据出错');
    		}
    	} else {
    		throw new \Exception('要加密的原始数据为空');
    	}
    }

   /**
     * [Base64Decrypt 私钥解密]
     * @author: edge
     * @time: 2017/10/22
     * @param string $str [description]
     */
    public function Base64Decrypt($str = '')
    {
    	if (!empty($str)) {
    		$str = base64_decode($str);
    		if(openssl_private_decrypt($str,$design,$this->rsa_private_key)){
    			$design = json_decode($design);
    			return $design;
    		} else {
    			throw new \Exception('解密数据出错');
    		}
    	} else {
    		throw new \Exception('要解密的数据为空');
    	}
    }

}

转载于:https://my.oschina.net/u/3004226/blog/1554615

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值