OpenSSL 公钥私钥加解密封装
<?php
/**
* OpenSSL 非对称加解密
*
* Class Signature
* @package utils
*/
class Signature
{
/**
* 创建公钥私钥配置
*
* @var array
*/
protected $createConfig = [
'config' => 'D:\phpStudy\PHPTutorial\Apache\conf\openssl.cnf',//openssl配置文件路径,使用的时候再具体配置
'digest_alg' => 'sha512',
'private_key_bits' => 1024, //字节数 512 1024 2048 4096 等
'private_key_type' => OPENSSL_KEYTYPE_RSA, //加密类型
];
/**
* 基本配置
*
* @var array
*/
protected $config = [
'private_key' => '',//私钥pem文件路径 例如: /var/www/test/app/ssh/private_key.pem
'public_key' => '',//公钥pem文件路径 例如:/var/www/test/app/ssh/rsa_public_key.pem
];
/**
* 公钥
*
* @var string
*/
protected $privateKey = '';
/**
* 私钥
*
* @var string
*/
protected $publicKey = '';
/**
* Signature constructor.
* @param array $config
*/
public function __construct(array $config = [])
{
if ($config){
$this->setConfig($config);
}
}
/**
* 设置配置
*
* @Author: kanin <990921093@qq.com>
* @Date: 2019/06/21 15:42
* @param $config
* @return $this
*/
public function setConfig($config)
{
$this->config = array_merge($this->config, $config);
//私钥
if (is_file($this->config['private_key'])) {
$this->privateKey = file_get_contents($this->config['private_key']);
}
//公钥
if (is_file($this->config['public_key'])) {
$this->publicKey = file_get_contents($this->config['public_key']);
}
return $this;
}
/**
* 创建私钥和公钥
*
* @Author: kanin <990921093@qq.com>
* @Date: 2019/06/21 15:53
* @param string $path
* @return array
* @throws \Exception
*/
public function createKey($path = '')
{
if (!$path) {
$path = $this->config['path'];
}
try {
//创建公钥和私钥 返回资源
$res = openssl_pkey_new($this->createConfig);
//从得到的资源中获取私钥,把私钥赋给$privateKey
openssl_pkey_export($res, $privateKey, null, $this->createConfig);
//从得到的资源中获取公钥,返回公钥$pubKey
$pubKey = openssl_pkey_get_details($res);
//公钥
$publicKey = $pubKey["key"];
//生成文件
$publicKeyPath = $path . 'rsa_public_key.pem';
$privateKeyPath = $path . 'private_key.pem';
if (!is_dir($path)) {
mkdir($path);
}
file_put_contents($publicKeyPath, $publicKey);
file_put_contents($privateKeyPath, $privateKey);
} catch (\Exception $exception) {
throw new \Exception($exception->getMessage(), 1001);
}
return compact('publicKeyPath', 'privateKeyPath');
}
/**
* 私钥加密
*
* @Author: kanin <990921093@qq.com>
* @Date: 2019/06/21 15:21
* @param $data
* @return string
* @throws \Exception
*/
public function privateKeyEncrypt(string $data)
{
if (!$this->privateKey) {
throw new \Exception('私钥未设置', 1002);
}
//使用私钥加密
openssl_private_encrypt($data, $encrypted, $this->privateKey);
//加密后通常都会有一些特殊字符,需要用base64_encode处理一下
return base64_encode($encrypted);
}
/**
* 公钥加密
*
* @Author: kanin <990921093@qq.com>
* @Date: 2019/06/21 15:42
* @param $data
* @return string
* @throws \Exception
*/
public function publicKeyEncrypt(string $data)
{
if (!$this->publicKey) {
throw new \Exception('公钥未设置', 1003);
}
//使用公钥加密
openssl_public_encrypt($data, $encrypted, $this->publicKey);
//加密后通常都会有一些特殊字符,需要用base64_encode处理一下
return base64_encode($encrypted);
}
/**
* 私钥解密
*
* @Author: kanin <990921093@qq.com>
* @Date: 2019/06/21 15:23
* @param $data
* @return bool|string
* @throws \Exception
*/
public function privateKeyDecrypt(string $data)
{
if (!$this->privateKey) {
throw new \Exception('私钥未设置', 1002);
}
//加密的时候使用了base64处理
$data = base64_decode($data);
//使用私钥解密
openssl_private_decrypt($data, $decrypted, $this->privateKey);
return $decrypted;
}
/**
* 公钥解密
*
* @Author: kanin <990921093@qq.com>
* @Date: 2019/06/21 15:25
* @param $data
* @return bool|string
* @throws \Exception
*/
public function publicKeyDecrypt(string $data)
{
if (!$this->publicKey) {
throw new \Exception('公钥未设置', 1003);
}
//加密的时候使用了base64处理
$data = base64_decode($data);
//使用公钥解密
openssl_public_decrypt($data, $decrypted, $this->publicKey);
return $decrypted;
}
}
使用
$config = [
'private_key'=>'/var/www/test/app/ssh/private_key.pem',
'public_key'=>'/var/www/test/app/ssh/rsa_public_key.pem',
];
$string = '测试';
$signature = new Signature($config);
$encrypted = $signature->publicKeyEncrypt($string);
$decrypted = $signature->privateKeyDecrypt($encrypted);
var_dump("公钥加密:".$encrypted);
var_dump("私钥解密:".$decrypted);
echo "------------------";
$encrypted = $signature->privateKeyEncrypt($string);
$decrypted = $signature->publicKeyDecrypt($encrypted);
var_dump("私钥加密:".$encrypted);
var_dump("公钥解密:".$decrypted);
本作品采用《CC 协议》,转载必须注明作者和本文链接