简介:使用非对称加密主要是借助openssl的公钥和私钥,用公钥加密私钥解密,或者私钥加密公钥解密。安装openssl和php的openssl扩展生成私钥:openssl genrsa 用于生成rsa私钥文件,生成是可以指定私钥长度和密码保护openss ...
|
使用非对称加密主要是借助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("要解密的数据为空"); } }}本文仅代表作者个人观点,不代表巅云官方发声,对观点有疑义请先联系作者本人进行修改,若内容非法请联系平台管理员,邮箱2522407257@qq.com。更多相关资讯,请到巅云www.rzxsoft.cn学习互联网营销技术请到巅云建站www.rzxsoft.cn。 |