php 非对称加密解密类
<?php
namespace app\Parentclient\model;
header("Content-Type: text/html;charset=utf-8");
// 让敲代码成为一种 ___"时尚"
//
// .::::.
// .::::::::.
// :::::::::::
// ..:::::::::::'
// '::::::::::::'
// .::::::::::
// '::::::::::::::..
// ..::::::::::::.
// ``::::::::::::::::
// ::::``:::::::::' .:::.
// ::::' ':::::' .::::::::.
// .::::' :::: .:::::::'::::.
// .:::' ::::: .:::::::::' ':::::.
// .::' :::::.:::::::::' ':::::.
// .::' ::::::::::::::' ``::::.
// ...::: ::::::::::::' ``::.
// ```` ':. ':::::::::' ::::..
// '.:::::' '::'````..
// 美女保佑 ———— 项目无BUG_
$cookieStr = $this->rsa->private_encrypt($token); /*si秘钥加密*/
一般用撕钥加密,用公钥解密
/*
生成公钥、私钥对,私钥加密的内容能通过公钥解密(反过来亦可以)
下载开源RSA密钥生成工具openssl(通常Linux系统都自带该程序),解压缩至独立的文件夹,进入其中的bin目录,执行以下命令:
openssl genrsa -out rsa_private_key.pem 1024 #生成原始 RSA私钥文件 rsa_private_key.pem
openssl pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt -out private_key.pem #将原始 RSA私钥转换为 pkcs8格式
openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem #通过私钥生成对应 RSA公钥 rsa_public_key.pem
*/
$private_key = '-----BEGIN PRIVATE KEY-----
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAOH2AFAc8C9BzJTr
AaxUsOyUEIoT+T6u/T3YAhIYVcvVrM46GKSfcWu/iCI95c3RZIzyYZGYPnn2DsPD
Sus+pUr1eZVxZRgTDc0usYMyAr+/XPFcOEOt9T/kMJoTHl/6BbUweSDGFCovIQ7X
xuN0pLZITGUdyodxw7hS3A+AsqxhAgMBAAECgYBUU8RmIYaZfErnQJUNRUYJ5HAP
t8iK1i94RwbuzdonCgVFqXTM6xwfBAnADa2XlA+cclBrT4/xYBy+fkGVbXmtvk1j
kGWuVMXaI9jxQjwYrbiA5tT+kaWF4Gh5SyUnAKgLXX40YMCc5Fi/vXOD523QDvko
gnDMDODfs/ELaccuKQJBAPHhTXeJfYWYqCyHi1wO5ZFzUccJCL2OWYdbkkXx3fGC
gvLu6ybIuECz8sDxmd3ZpXUxo6n0mw2kA3TrfE6cf6cCQQDvJszYaddxwpx1yopB
dGBjXYGyOIf4CrZR9ryx3xfRIapNA7X6g5flMrltBo0yCqWtTmJKmmydQ3nzWOUk
nrS3AkA+8/W2k+lAeR/kHw8NUa88MPhqxhYpR8BTc2jZnX63WdF9KuO2/2y9Ilif
EQKL9BaVLus607f7T+G6b4O17hNnAkEA6teLKKkm64tT4Ue3ZHdHf8v+5Ggm/Shn
yS0mNvUAilkoxt/x/8LhkiyxcaKRXKaf8wOZ+fCS4Au24YRV4BQJGQJBALFDXGIT
mm23KFaIMC2Au/gpO5CBmwDkbdZaQXl6K6B/Vje6a3ke7mIiKCmkcG7kdax0LUta
1xJYypi0HZCq/0A=
-----END PRIVATE KEY-----';
$public_key = '-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDh9gBQHPAvQcyU6wGsVLDslBCK
E/k+rv092AISGFXL1azOOhikn3Frv4giPeXN0WSM8mGRmD559g7Dw0rrPqVK9XmV
cWUYEw3NLrGDMgK/v1zxXDhDrfU/5DCaEx5f+gW1MHkgxhQqLyEO18bjdKS2SExl
HcqHccO4UtwPgLKsYQIDAQAB
-----END PUBLIC KEY-----';
file_put_contents('public_key.txt', $public_key);
file_put_contents('private_key.txt', $private_key);
/**
* rsa 非对称加解密
*/
class rsa {
private $public_key = ''; //公密钥
private $private_key = ''; //私密钥
private $public_key_resource = ''; //公密钥资源
private $private_key_resource = ''; //私密钥资源
/**
* 架构函数
* @param [string] $public_key_file [公密钥文件地址]
* @param [string] $private_key_file [私密钥文件地址]
*/
public function __construct($public_key_file,$private_key_file) {
try {
if(!file_exists($public_key_file) || !file_exists($private_key_file)) {
throw new Exception('key file no exists');
}
if (false == ($this->public_key = file_get_contents($public_key_file)) || false == ($this->private_key = file_get_contents($private_key_file))) {
throw new Exception('read key file fail');
}
if(false == ($this->public_key_resource = $this->is_bad_public_key($this->public_key)) || false == ($this->private_key_resource = $this->is_bad_private_key($this->private_key))) {
throw new Exception('public key or private key no usable');
}
} catch (Exception $e) {
die($e->getMessage());
}
}
private function is_bad_public_key($public_key) {
return openssl_pkey_get_public($public_key);
}
private function is_bad_private_key($private_key) {
return openssl_pkey_get_private($private_key);
}
/**
* 生成一对公私密钥 成功返回 公私密钥数组 失败 返回 false
*/
public function create_key() {
$res = openssl_pkey_new();
if($res == false) return false;
openssl_pkey_export($res, $private_key);
$public_key = openssl_pkey_get_details($res);
return array('public_key'=>$public_key["key"],'private_key'=>$private_key);
}
/**
* 用私密钥加密
*/
// public function private_encrypt($input) {
// openssl_private_encrypt($input,$output,$this->private_key_resource);
// return base64_encode($output);
// }
//私钥加密
public function private_encrypt($data){
$crypto = '';
foreach (str_split($data, 117) as $chunk) {
openssl_private_encrypt($chunk, $encryptData, $this->private_key_resource);
$crypto .= $encryptData;
}
$encrypted = $this->urlsafe_b64encode($crypto);//加密后的内容通常含有特殊字符,需要编码转换下,在网络间通过url传输时要注意base64编码是否是url安全的
return $encrypted;
}
//加密码时把特殊符号替换成URL可以带的内容
function urlsafe_b64encode($string) {
$data = base64_encode($string);
$data = str_replace(array('+','/','='),array('-','_',''),$data);
return $data;
}
//解密码时把转换后的符号替换特殊符号
function urlsafe_b64decode($string) {
$data = str_replace(array('-','_'),array('+','/'),$string);
$mod4 = strlen($data) % 4;
if ($mod4) {
$data .= substr('====', $mod4);
}
return base64_decode($data);
}
//私钥加密的内容通过公钥可用解密出来
public function public_decrypt($encrypted){
$crypto = '';
foreach (str_split($this->urlsafe_b64decode($encrypted), 128) as $chunk) {
openssl_public_decrypt($chunk, $decryptData, $this->public_key_resource);
$crypto .= $decryptData;
}
return $crypto;
}
/**
* 解密 私密钥加密后的密文
*/
// public function public_decrypt($input) {
// openssl_public_decrypt(base64_decode($input),$output,$this->public_key_resource);
// return $input;
// }
/**
* 用公密钥加密
*/
public function public_encrypt($input) {
openssl_public_encrypt($input,$output,$this->public_key_resource);
return base64_encode($output);
}
/**
* 解密 公密钥加密后的密文
*/
public function private_decrypt($input) {
openssl_private_decrypt(base64_decode($input),$output,$this->private_key_resource);
return $output;
}
}
// $time = time();
// echo '时间戳=='.$time.'<br>';
// $rsa = new rsa('public_key.txt','private_key.txt');
// $str = '撇如惊鸿&'.$time;
// $str = $rsa->public_encrypt($str); //用公密钥加密
// echo '公秘钥加密=='.$str.'</br>';
// $str = $rsa->private_decrypt($str); //用私密钥解密
// echo '私秘钥解密=='.$str.'</br>';
// //=============================================================
// $str = $rsa->private_encrypt($str); //用丝密钥加密
// echo '私密钥加密=='.$str.'</br>';
// $str = $rsa->public_decrypt($str); //用公密钥解密
// echo '公密钥解密=='.$str.'</br>';