1.7
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
/**
|
||||
* 证书信息查看
|
||||
*/
|
||||
|
||||
namespace plugin\dev\cert;
|
||||
|
||||
use app\Plugin;
|
||||
use Exception;
|
||||
|
||||
error_reporting(0);
|
||||
|
||||
class App extends Plugin
|
||||
{
|
||||
|
||||
private static $key_type = ['RSA', 'DSA', 'DH', 'EC'];
|
||||
|
||||
public function index()
|
||||
{
|
||||
return $this->view();
|
||||
}
|
||||
|
||||
public function query(){
|
||||
if(input('post.type/d') == 2){
|
||||
$domain = input('post.domain', null, 'trim');
|
||||
if(!$domain) return msg('error','no domain');
|
||||
$port = 443;
|
||||
if(strpos($domain, ':')){
|
||||
$a = explode(':', $domain);
|
||||
$domain = $a[0];
|
||||
if(is_numeric($a[1])) $port = intval($a[1]);
|
||||
}
|
||||
if(!checkdomain($domain)) return msg('error','域名格式不正确');
|
||||
try{
|
||||
$cert = $this->getSSLCertificate($domain, $port);
|
||||
}catch(Exception $e){
|
||||
return msg('error', $e->getMessage());
|
||||
}
|
||||
}else{
|
||||
$cert = input('post.cert');
|
||||
if(!$cert) return msg('error','no cert');
|
||||
}
|
||||
|
||||
$arr = openssl_x509_parse($cert);
|
||||
if(!$arr){
|
||||
return msg('error', '证书文件错误');
|
||||
}
|
||||
$pkey_detail = openssl_pkey_get_details(openssl_pkey_get_public($cert));
|
||||
|
||||
$subject_info = [
|
||||
'通用名称(CN)' => $this->format($arr['subject']['CN']),
|
||||
'国家(C)' => $this->format($arr['subject']['C']),
|
||||
'省份(S)' => $this->format($arr['subject']['ST']),
|
||||
'城市(L)' => $this->format($arr['subject']['L']),
|
||||
'组织(O)' => $this->format($arr['subject']['O']),
|
||||
'部门(OU)' => $this->format($arr['subject']['OU']),
|
||||
];
|
||||
$issuer_info = [
|
||||
'通用名称(CN)' => $this->format($arr['issuer']['CN']),
|
||||
'国家(C)' => $this->format($arr['issuer']['C']),
|
||||
'组织(O)' => $this->format($arr['issuer']['O']),
|
||||
];
|
||||
$cert_info = [
|
||||
'序列号' => $arr['serialNumberHex'],
|
||||
'签名算法' => $arr['signatureTypeSN'],
|
||||
'密钥类型' => isset(self::$key_type[$pkey_detail['type']]) ? self::$key_type[$pkey_detail['type']] : '未知',
|
||||
'密钥强度' => $pkey_detail['bits'],
|
||||
'颁发时间' => date('Y-m-d H:i:s', $arr['validFrom_time_t']),
|
||||
'过期时间' => date('Y-m-d H:i:s', $arr['validTo_time_t']),
|
||||
'有效期' => round(($arr['validTo_time_t']-time()) / 3600 / 24).'天',
|
||||
'备用名' => $arr['extensions']['subjectAltName'],
|
||||
];
|
||||
|
||||
return msg('ok','success',['subject_info'=>$subject_info, 'issuer_info'=>$issuer_info, 'cert_info'=>$cert_info]);
|
||||
}
|
||||
|
||||
private function format($mixed){
|
||||
if(is_array($mixed)){
|
||||
return $mixed[array_key_first($mixed)];
|
||||
}
|
||||
return $mixed;
|
||||
}
|
||||
|
||||
private function getSSLCertificate($domain, $port = 443) {
|
||||
$context = stream_context_create([
|
||||
"ssl" => [
|
||||
"capture_peer_cert" => true,
|
||||
],
|
||||
]);
|
||||
|
||||
$socket = stream_socket_client('ssl://'.$domain.':'.$port, $errno, $errstr, 10, STREAM_CLIENT_CONNECT, $context);
|
||||
|
||||
if ($socket) {
|
||||
$params = stream_context_get_params($socket);
|
||||
if(!isset($params["options"]["ssl"]["peer_certificate"])){
|
||||
throw new Exception('域名未找到SSL证书信息');
|
||||
}
|
||||
fclose($socket);
|
||||
return $params["options"]["ssl"]["peer_certificate"];
|
||||
} else {
|
||||
$errstr = mb_convert_encoding($errstr, 'UTF-8', 'GB2312');
|
||||
throw new Exception('域名443端口连接失败,'.$errstr);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
{extend name="common/plugin_layout" /}
|
||||
{block name="title"}{$plugin.title} - {:config_get('title')}{/block}
|
||||
{block name="main"}
|
||||
<style>
|
||||
.query-title{text-align: right; }
|
||||
@media (min-width: 576px) { .query-title{width: 150px;} }
|
||||
</style>
|
||||
<div class="container-xl" id="app">
|
||||
<div class="col-sm-12 col-md-10 col-xl-8 center-block">
|
||||
<div class="card card-preview">
|
||||
<div class="card-inner mt-3">
|
||||
<div class="nya-title nk-ibx-action-item progress-rating">
|
||||
<span class="nk-menu-text font-weight-bold">证书信息查看</span>
|
||||
</div>
|
||||
<div class="form-group mt-3">
|
||||
<div class="form-control-wrap">
|
||||
<div class="custom-control custom-radio mr-3">
|
||||
<input type="radio" class="custom-control-input" v-model="inputtype" id="customRadio1" :value="0">
|
||||
<label class="custom-control-label" for="customRadio1">粘贴文件内容</label>
|
||||
</div>
|
||||
<div class="custom-control custom-radio mr-3">
|
||||
<input type="radio" class="custom-control-input" v-model="inputtype" id="customRadio2" :value="1">
|
||||
<label class="custom-control-label" for="customRadio2">上传文件</label>
|
||||
</div>
|
||||
<div class="custom-control custom-radio">
|
||||
<input type="radio" class="custom-control-input" v-model="inputtype" id="customRadio3" :value="2">
|
||||
<label class="custom-control-label" for="customRadio3">输入域名</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group" v-show="inputtype==0">
|
||||
<label class="form-label">证书PEM文件:</label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" rows="6" v-model="cert" placeholder="-----BEGIN CERTIFICATE-----"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group" v-show="inputtype==1">
|
||||
<label class="form-label">证书PEM文件:</label>
|
||||
<div class="form-control-wrap">
|
||||
<div class="custom-file">
|
||||
<input type="file" class="custom-file-input" id="customFile1" accept=".pem,.crt,.cer" required @change="handleFile">
|
||||
<label class="custom-file-label" for="customFile1">点击选择文件(.pem .crt .cer)</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group" v-show="inputtype==2">
|
||||
<label class="form-label">查询域名的SSL证书信息:</label>
|
||||
<div class="form-control-wrap">
|
||||
<input type="text" v-model="domain" placeholder="请输入支持https访问的域名,支持加端口" class="form-control form-control-lg" @keyup.enter="query" ref="input" autocomplete="off">
|
||||
</div>
|
||||
</div>
|
||||
<button class="btn btn-dim btn-outline-primary btn-block card-link mb-3" @click="query">
|
||||
查看证书信息
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card card-preview" v-show="showresult" style="display:none">
|
||||
<div class="card-inner mt-3">
|
||||
<div class="nya-title nk-ibx-action-item progress-rating">
|
||||
<span class="nk-menu-text font-weight-bold">查询结果</span>
|
||||
</div>
|
||||
<h6>主题信息</h6>
|
||||
<div class="table-responsive mb-2">
|
||||
<table class="table table-hover table-bordered">
|
||||
<tbody>
|
||||
<tr v-for="(item,index) in subject_info"><td class="query-title">{{index}}</td><td>{{item}}</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<h6>签发者信息</h6>
|
||||
<div class="table-responsive mb-2">
|
||||
<table class="table table-hover table-bordered">
|
||||
<tbody>
|
||||
<tr v-for="(item,index) in issuer_info"><td class="query-title">{{index}}</td><td>{{item}}</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<h6>证书信息</h6>
|
||||
<div class="table-responsive">
|
||||
<table class="table table-hover table-bordered">
|
||||
<tbody>
|
||||
<tr v-for="(item,index) in cert_info"><td class="query-title">{{index}}</td><td>{{item}}</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{/block}
|
||||
{block name="script"}
|
||||
<script src="{$cdn_cdnjs}vue/2.6.14/vue.min.js"></script>
|
||||
<script>
|
||||
new Vue({
|
||||
el: '#app',
|
||||
data: {
|
||||
inputtype: 0,
|
||||
cert: '',
|
||||
domain: '',
|
||||
showresult: false,
|
||||
subject_info: [],
|
||||
issuer_info: [],
|
||||
cert_info: [],
|
||||
},
|
||||
methods: {
|
||||
handleFile(event) {
|
||||
var file = event.target.files[0];
|
||||
layer.load(0, {shade:0.1});
|
||||
var that = this;
|
||||
that.cert = '';
|
||||
var reader = new FileReader();
|
||||
reader.onload = () => {
|
||||
layer.closeAll();
|
||||
var cert = reader.result;
|
||||
if(cert.indexOf('BEGIN CERTIFICATE') == -1){
|
||||
layer.alert('证书文件错误', {icon: 5});return;
|
||||
}
|
||||
that.cert = cert;
|
||||
};
|
||||
reader.onerror = () => {
|
||||
layer.closeAll();
|
||||
layer.alert('证书文件读取失败', {icon: 5});return;
|
||||
};
|
||||
reader.readAsText(file);
|
||||
},
|
||||
query() {
|
||||
if(this.inputtype == 2){
|
||||
if(this.domain == ''){
|
||||
layer.alert('域名不能为空');return;
|
||||
}
|
||||
this.checkURL();
|
||||
}else if(this.inputtype < 2 && this.cert == ''){
|
||||
layer.alert('证书内容不能为空');return;
|
||||
}
|
||||
layer.load(0, {shade:0.1});
|
||||
var that=this;
|
||||
$.ajax({
|
||||
url: '/api/{$plugin.alias}/query',
|
||||
type: 'post',
|
||||
dataType: 'json',
|
||||
data: {type: that.inputtype, cert: that.cert, domain: that.domain},
|
||||
cache: false,
|
||||
success: function (data) {
|
||||
layer.closeAll();
|
||||
if(data.status=='ok'){
|
||||
that.subject_info = data.data.subject_info;
|
||||
that.issuer_info = data.data.issuer_info;
|
||||
that.cert_info = data.data.cert_info;
|
||||
that.showresult = true;
|
||||
}else{
|
||||
layer.alert(data.message, {icon: 5});
|
||||
}
|
||||
},
|
||||
error: function () {
|
||||
layer.closeAll();
|
||||
layer.msg('服务器错误', {icon: 5});
|
||||
}
|
||||
});
|
||||
},
|
||||
checkURL()
|
||||
{
|
||||
var url = this.domain.trim();
|
||||
if (url.indexOf(" ")>=0){
|
||||
url = url.replace(/ /g,"");
|
||||
}
|
||||
if (url.toLowerCase().indexOf("http://")==0){
|
||||
url = url.slice(7);
|
||||
}
|
||||
if (url.toLowerCase().indexOf("https://")==0){
|
||||
url = url.slice(8);
|
||||
}
|
||||
if (url.slice(url.length-1)=="/"){
|
||||
url = url.slice(0,url.length-1);
|
||||
}
|
||||
this.domain = url;
|
||||
},
|
||||
},
|
||||
})
|
||||
</script>
|
||||
{/block}
|
||||
@@ -0,0 +1,210 @@
|
||||
<?php
|
||||
/**
|
||||
* RSA证书格式转换
|
||||
*/
|
||||
|
||||
namespace plugin\dev\cert_convert;
|
||||
|
||||
use app\Plugin;
|
||||
use CURLFile;
|
||||
use Exception;
|
||||
|
||||
class App extends Plugin
|
||||
{
|
||||
|
||||
private static $apiurl = 'http://keytool.odata.cc/api.php';
|
||||
|
||||
public function index()
|
||||
{
|
||||
return $this->view();
|
||||
}
|
||||
|
||||
public function convert()
|
||||
{
|
||||
$from = input('post.from');
|
||||
$to = input('post.to');
|
||||
if(empty($from) || empty($to)) return msg('error', '必填项不能为空');
|
||||
|
||||
try{
|
||||
$params = [];
|
||||
if($from == 'PEM' && $to != 'DER' && $to != 'PKCS7'){
|
||||
if(!isset($_FILES['private'])) return msg('error', 'PEM私钥文件不存在');
|
||||
$private_path = $_FILES['private']['tmp_name'];
|
||||
$this->check_cert('PEM私钥', $private_path);
|
||||
|
||||
if(!isset($_FILES['cert'])) return msg('error', 'PEM证书文件不存在');
|
||||
$cert_path = $_FILES['cert']['tmp_name'];
|
||||
$this->check_cert('PEM证书', $cert_path);
|
||||
|
||||
if(isset($_FILES['cacert'])){
|
||||
$cacert_path = $_FILES['cacert']['tmp_name'];
|
||||
$this->check_cert('PEM证书', $cacert_path);
|
||||
file_put_contents($cert_path, PHP_EOL.file_get_contents($cacert_path), FILE_APPEND);
|
||||
}
|
||||
|
||||
$params['private'] = new CURLFile($private_path);
|
||||
$params['cert'] = new CURLFile($cert_path);
|
||||
$params['pass'] = input('post.pass', null, 'trim');
|
||||
$this->check_pass('证书密码', $params['pass'], true);
|
||||
$act = $to == 'JKS' ? 'pem_to_jks' : 'pem_to_pfx';
|
||||
|
||||
}elseif($from == 'PEM' && $to == 'DER'){
|
||||
if(!isset($_FILES['cert'])) return msg('error', 'PEM证书文件不存在');
|
||||
$cert_path = $_FILES['cert']['tmp_name'];
|
||||
$this->check_cert('PEM证书', $cert_path);
|
||||
$params['cert'] = new CURLFile($cert_path);
|
||||
$act = 'pem_to_der';
|
||||
|
||||
}elseif($from == 'DER' && $to == 'PEM'){
|
||||
if(!isset($_FILES['cert'])) return msg('error', 'DER证书文件不存在');
|
||||
$cert_path = $_FILES['cert']['tmp_name'];
|
||||
$this->check_cert('DER证书', $cert_path);
|
||||
$params['cert'] = new CURLFile($cert_path);
|
||||
$act = 'der_to_pem';
|
||||
|
||||
}elseif($from == 'PKCS12'){
|
||||
if(!isset($_FILES['cert'])) return msg('error', 'PKCS12证书文件不存在');
|
||||
$cert_path = $_FILES['cert']['tmp_name'];
|
||||
$this->check_cert('PKCS12证书', $cert_path);
|
||||
$params['cert'] = new CURLFile($cert_path);
|
||||
if($to == 'JKS'){
|
||||
$params['srcpass'] = input('post.srcpass', null, 'trim');
|
||||
$params['destpass'] = input('post.destpass', null, 'trim');
|
||||
$this->check_pass('原证书密码', $params['srcpass'], true);
|
||||
$this->check_pass('新证书密码', $params['destpass']);
|
||||
$act = 'pfx_to_jks';
|
||||
}elseif($to == 'PEM'){
|
||||
$params['pass'] = input('post.pass', null, 'trim');
|
||||
$this->check_pass('原证书密码', $params['pass'], true);
|
||||
$act = 'pfx_to_pem';
|
||||
}
|
||||
|
||||
}elseif($from == 'JKS'){
|
||||
if(!isset($_FILES['cert'])) return msg('error', 'JKS证书文件不存在');
|
||||
$cert_path = $_FILES['cert']['tmp_name'];
|
||||
$this->check_cert('JKS证书', $cert_path);
|
||||
$params['cert'] = new CURLFile($cert_path);
|
||||
if($to == 'PKCS12'){
|
||||
$params['srcpass'] = input('post.srcpass', null, 'trim');
|
||||
$params['destpass'] = input('post.destpass', null, 'trim');
|
||||
$this->check_pass('原证书密码', $params['srcpass']);
|
||||
$this->check_pass('新证书密码', $params['destpass'], true);
|
||||
if(empty($params['destpass'])) $params['destpass'] = $params['srcpass'];
|
||||
$act = 'jks_to_pfx';
|
||||
}elseif($to == 'PEM'){
|
||||
$params['pass'] = input('post.pass', null, 'trim');
|
||||
$this->check_pass('原证书密码', $params['pass']);
|
||||
$act = 'jks_to_pem';
|
||||
}
|
||||
|
||||
}elseif($from == 'PEM' && $to == 'PKCS7'){
|
||||
if(!isset($_FILES['cert'])) return msg('error', 'PEM证书文件不存在');
|
||||
$cert_path = $_FILES['cert']['tmp_name'];
|
||||
$this->check_cert('PEM证书', $cert_path);
|
||||
|
||||
if(isset($_FILES['cacert'])){
|
||||
$cacert_path = $_FILES['cacert']['tmp_name'];
|
||||
$this->check_cert('PEM证书', $cacert_path);
|
||||
file_put_contents($cert_path, PHP_EOL.file_get_contents($cacert_path), FILE_APPEND);
|
||||
}
|
||||
|
||||
$params['cert'] = new CURLFile($cert_path);
|
||||
$act = 'pem_to_p7b';
|
||||
|
||||
}elseif($from == 'PKCS7' && $to == 'PEM'){
|
||||
if(!isset($_FILES['cert'])) return msg('error', 'PKCS7证书文件不存在');
|
||||
$cert_path = $_FILES['cert']['tmp_name'];
|
||||
$this->check_cert('PKCS7证书', $cert_path);
|
||||
$params['cert'] = new CURLFile($cert_path);
|
||||
$act = 'p7b_to_pem';
|
||||
|
||||
}else{
|
||||
return msg('error', '转换类型不存在');
|
||||
}
|
||||
|
||||
$response = $this->requestApi($act, $params);
|
||||
|
||||
$files = [];
|
||||
if($to == 'PEM' && $from != 'DER' && $from != 'PKCS7' && substr($response, 0, 1) == '{'){
|
||||
$arr = json_decode($response, true);
|
||||
$files[] = ['name'=>'PEM 服务器证书', 'file'=>'cert.pem', 'data'=>base64_encode($arr['cert'])];
|
||||
if(!empty($arr['extra_certs'])){
|
||||
$cacert = implode(PHP_EOL, $arr['extra_certs']);
|
||||
$files[] = ['name'=>'PEM 中间/根证书', 'file'=>'cacert.pem', 'data'=>base64_encode($cacert)];
|
||||
}
|
||||
$files[] = ['name'=>'PEM 私钥', 'file'=>'privatekey.pem', 'data'=>base64_encode($arr['private_key'])];
|
||||
$files[] = ['name'=>'PEM 公钥', 'file'=>'publickey.pem', 'data'=>base64_encode($arr['public_key'])];
|
||||
}elseif($to == 'PEM'){
|
||||
$files[] = ['name'=>'PEM 证书文件', 'file'=>'cert.pem', 'data'=>base64_encode($response)];
|
||||
}elseif($to == 'DER'){
|
||||
$files[] = ['name'=>'DER 证书文件', 'file'=>'cert.der', 'data'=>base64_encode($response)];
|
||||
}elseif($to == 'PKCS12'){
|
||||
$files[] = ['name'=>'PKCS12 证书文件', 'file'=>'cert.pfx', 'data'=>base64_encode($response)];
|
||||
}elseif($to == 'JKS'){
|
||||
$files[] = ['name'=>'JKS 证书文件', 'file'=>'cert.jks', 'data'=>base64_encode($response)];
|
||||
}elseif($to == 'PKCS7'){
|
||||
$files[] = ['name'=>'PKCS7 证书文件', 'file'=>'cert.p7b', 'data'=>base64_encode($response)];
|
||||
}
|
||||
|
||||
return msg('ok', 'success', ['files'=>$files]);
|
||||
|
||||
}catch(Exception $e){
|
||||
return msg('error', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private function check_cert($name, $path){
|
||||
$file_size = filesize($path);
|
||||
if($file_size < 100 || $file_size > 50 * 1024){
|
||||
throw new Exception($name.'文件大小超过限制');
|
||||
}
|
||||
}
|
||||
|
||||
private function check_pass($name, $value, $empty = false){
|
||||
if($empty && empty($value)) return;
|
||||
if(empty($value)){
|
||||
throw new Exception($name.'不能为空');
|
||||
}
|
||||
if(strlen($value) < 6){
|
||||
throw new Exception($name.'至少6个字符');
|
||||
}
|
||||
if(strlen($value) > 32){
|
||||
throw new Exception($name.'最多32个字符');
|
||||
}
|
||||
}
|
||||
|
||||
private function requestApi($act, $params){
|
||||
$requrl = self::$apiurl . '?act=' . $act;
|
||||
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $requrl);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
||||
$httpheader[] = "Accept: */*";
|
||||
$httpheader[] = "Accept-Encoding: gzip,deflate,sdch";
|
||||
$httpheader[] = "Accept-Language: zh-CN,zh;q=0.8";
|
||||
$httpheader[] = "Connection: close";
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
|
||||
curl_setopt($ch, CURLOPT_POST, 1);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
|
||||
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
$response = curl_exec($ch);
|
||||
|
||||
if (curl_errno($ch) > 0) {
|
||||
curl_close($ch);
|
||||
throw new Exception('接口请求失败');
|
||||
}
|
||||
|
||||
$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
if ($httpStatusCode != 200) {
|
||||
curl_close($ch);
|
||||
$arr = json_decode($response, true);
|
||||
throw new Exception($arr ? $arr['msg'] : '接口返回数据解析失败');
|
||||
}
|
||||
|
||||
curl_close($ch);
|
||||
return $response;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,327 @@
|
||||
{extend name="common/plugin_layout" /}
|
||||
{block name="title"}{$plugin.title} - {:config_get('title')}{/block}
|
||||
{block name="main"}
|
||||
<div class="container-xl" id="app">
|
||||
<div class="col-sm-12 col-md-10 col-xl-8 center-block">
|
||||
<div class="card card-preview">
|
||||
<div class="card-inner mt-3">
|
||||
<div class="nya-title nk-ibx-action-item progress-rating">
|
||||
<span class="nk-menu-text font-weight-bold">RSA证书格式转换</span>
|
||||
</div>
|
||||
<div class="d-flex flex-wrap">
|
||||
<div class="form-group flex-fill mr-2">
|
||||
<label class="form-label">从格式 <span class="text-danger">*</span></label>
|
||||
<div class="form-control-wrap">
|
||||
<select class="form-control" v-model="input.from">
|
||||
<option value="">请选择</option>
|
||||
<option v-for="value in convert_from" :value="value">{{value}}</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group flex-fill">
|
||||
<label class="form-label">转换至 <span class="text-danger">*</span></label>
|
||||
<div class="form-control-wrap">
|
||||
<select class="form-control" v-model="input.to">
|
||||
<option value="">请选择</option>
|
||||
<option v-for="value in convert_to" :value="value">{{value}}</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group" v-show="input.from=='PEM'&&input.to!='DER'&&input.to!='PKCS7'">
|
||||
<label class="form-label" for="customFileLabel">PEM 私钥文件 <span class="text-danger">*</span></label>
|
||||
<div class="form-control-wrap">
|
||||
<div class="custom-file">
|
||||
<input type="file" class="custom-file-input" id="customFile1" accept=".pem,.key" required @change="handleFile('pem_private', event)">
|
||||
<label class="custom-file-label" for="customFile1">点击选择文件</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group" v-show="input.from=='PEM'&&input.to!='DER'">
|
||||
<label class="form-label" for="customFileLabel">PEM 服务器证书文件 <span class="text-danger">*</span></label>
|
||||
<div class="form-control-wrap">
|
||||
<div class="custom-file">
|
||||
<input type="file" class="custom-file-input" id="customFile2" accept=".pem,.crt,.cer" required @change="handleFile('pem_mycert', event)">
|
||||
<label class="custom-file-label" for="customFile2">点击选择文件</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group" v-show="input.from=='PEM'&&input.to!='DER'">
|
||||
<label class="form-label" for="customFileLabel">PEM 中间/根证书文件</label>
|
||||
<div class="form-control-wrap">
|
||||
<div class="custom-file">
|
||||
<input type="file" class="custom-file-input" id="customFile3" accept=".pem,.crt,.cer" @change="handleFile('pem_cacert', event)">
|
||||
<label class="custom-file-label" for="customFile3">点击选择文件(可选)</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group" v-show="input.from=='PEM'&&input.to=='PKCS12'">
|
||||
<label class="form-label">PKCS12 证书密码</label>
|
||||
<div class="form-control-wrap">
|
||||
<input type="text" v-model="input.pkcs12_pass" placeholder="密码可为空" class="form-control">
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group" v-show="input.from=='PEM'&&input.to=='JKS'">
|
||||
<label class="form-label">JKS 证书密码 <span class="text-danger">*</span></label>
|
||||
<div class="form-control-wrap">
|
||||
<input type="text" v-model="input.jks_pass" placeholder="" class="form-control" required>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group" v-show="input.from=='PKCS12'">
|
||||
<label class="form-label" for="customFileLabel">PKCS12 证书文件 <span class="text-danger">*</span></label>
|
||||
<div class="form-control-wrap">
|
||||
<div class="custom-file">
|
||||
<input type="file" class="custom-file-input" id="customFile4" accept=".pfx,.p12" required @change="handleFile('pkcs12_cert', event)">
|
||||
<label class="custom-file-label" for="customFile4">点击选择文件(支持pfx、p12)</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="d-flex flex-wrap" v-if="input.from=='PKCS12'">
|
||||
<div class="form-group flex-fill mr-2">
|
||||
<label class="form-label">原PKCS12证书密码</label>
|
||||
<div class="form-control-wrap">
|
||||
<input type="text" v-model="input.pkcs12_pass" placeholder="没有密码请留空" class="form-control">
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group flex-fill" v-show="input.to=='JKS'">
|
||||
<label class="form-label">新JKS证书密码 <span class="text-danger">*</span></label>
|
||||
<div class="form-control-wrap">
|
||||
<input type="text" v-model="input.jks_pass" placeholder="填写JKS证书密码" class="form-control" required>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group" v-show="input.from=='JKS'">
|
||||
<label class="form-label" for="customFileLabel">JKS 证书文件 <span class="text-danger">*</span></label>
|
||||
<div class="form-control-wrap">
|
||||
<div class="custom-file">
|
||||
<input type="file" class="custom-file-input" id="customFile5" accept=".jks,.ks,.keystore" required @change="handleFile('jks_cert', event)">
|
||||
<label class="custom-file-label" for="customFile5">点击选择文件(支持jks、ks、keystore)</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="d-flex flex-wrap" v-if="input.from=='JKS'">
|
||||
<div class="form-group flex-fill">
|
||||
<label class="form-label">原JKS证书密码 <span class="text-danger">*</span></label>
|
||||
<div class="form-control-wrap">
|
||||
<input type="text" v-model="input.jks_pass" placeholder="填写JKS证书密码" class="form-control" required>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group flex-fill ml-2" v-show="input.to=='PKCS12'">
|
||||
<label class="form-label">新PKCS12证书密码</label>
|
||||
<div class="form-control-wrap">
|
||||
<input type="text" v-model="input.pkcs12_pass" placeholder="留空则与原密码相同" class="form-control">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group" v-show="input.from=='PEM'&&input.to=='DER'">
|
||||
<label class="form-label" for="customFileLabel">PEM 证书文件 <span class="text-danger">*</span></label>
|
||||
<div class="form-control-wrap">
|
||||
<div class="custom-file">
|
||||
<input type="file" class="custom-file-input" id="customFile6" accept=".pem,.crt,.cer" required @change="handleFile('pem_cert', event)">
|
||||
<label class="custom-file-label" for="customFile6">点击选择文件</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group" v-show="input.from=='DER'">
|
||||
<label class="form-label" for="customFileLabel">DER 证书文件 <span class="text-danger">*</span></label>
|
||||
<div class="form-control-wrap">
|
||||
<div class="custom-file">
|
||||
<input type="file" class="custom-file-input" id="customFile7" accept=".der,.crt,.cer" required @change="handleFile('der_cert', event)">
|
||||
<label class="custom-file-label" for="customFile7">点击选择文件</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group" v-show="input.from=='PKCS7'">
|
||||
<label class="form-label" for="customFileLabel">PKCS7 证书文件 <span class="text-danger">*</span></label>
|
||||
<div class="form-control-wrap">
|
||||
<div class="custom-file">
|
||||
<input type="file" class="custom-file-input" id="customFile8" accept=".p7b,.p7c,.spc" required @change="handleFile('pkcs7_cert', event)">
|
||||
<label class="custom-file-label" for="customFile8">点击选择文件(支持p7b、p7c、spc)</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<button class="btn btn-dim btn-outline-secondary btn-block card-link mb-3" @click="submit">
|
||||
开始转换
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card card-preview" v-show="show_result" style="display:none">
|
||||
<div class="card-inner mt-3">
|
||||
<div class="nya-title nk-ibx-action-item progress-rating">
|
||||
<span class="nk-menu-text font-weight-bold">转换结果</span>
|
||||
</div>
|
||||
<ul class="list-group">
|
||||
<li class="list-group-item" v-for="(item,index) in result_file"><b>{{item.name}}:</b><a href="javascript:" @click="download(index)"><em class="icon ni ni-download"></em>点击下载</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card card-preview">
|
||||
<div class="card-inner">
|
||||
<h6><em class="icon ni ni-info"></em> 工具说明</h6>
|
||||
<div class="accordion-inner">
|
||||
<p>本工具支持PEM、DER、PKCS12、PKCS7、JKS等格式的证书转换</p>
|
||||
<p><b>证书格式介绍</b></p>
|
||||
<p><b>PEM:</b>以base64的形式存放证书、私钥,常见后缀是.pem .crt .cer .key</p>
|
||||
<p><b>DER:</b>以二进制的形式存放证书、私钥,常见后缀是.der .crt .cer</p>
|
||||
<p><b>PKCS#12:</b>以二进制的形式存放证书+私钥,通常包含保护密码,常见后缀是.pfx .p12</p>
|
||||
<p><b>PKCS#7:</b>以树状展示证书链,也支持单个证书,不含私钥,常见后缀是.p7b .p7c .spc</p>
|
||||
<p><b>JKS:</b>是Java平台使用的证书存储库文件,存放证书+私钥,有密码保护,常见后缀是.jks .ks .keystore</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{/block}
|
||||
{block name="script"}
|
||||
<script src="{$cdn_cdnjs}vue/2.6.14/vue.min.js"></script>
|
||||
<script src="{$cdn_cdnjs}FileSaver.js/2.0.5/FileSaver.min.js"></script>
|
||||
<script>
|
||||
new Vue({
|
||||
el: '#app',
|
||||
data: {
|
||||
mode: 0,
|
||||
convert_from: [
|
||||
'PEM',
|
||||
'DER',
|
||||
'PKCS12',
|
||||
'PKCS7',
|
||||
'JKS',
|
||||
],
|
||||
convert_to: [
|
||||
|
||||
],
|
||||
input: {
|
||||
from:'',
|
||||
to:'',
|
||||
pem_private: null,
|
||||
pem_mycert: null,
|
||||
pem_cacert: null,
|
||||
pkcs12_pass: '',
|
||||
jks_pass: '',
|
||||
pkcs12_cert: null,
|
||||
jks_cert: null,
|
||||
pem_cert: null,
|
||||
der_cert: null,
|
||||
pkcs7_cert: null,
|
||||
},
|
||||
show_result: false,
|
||||
result_file: [
|
||||
]
|
||||
},
|
||||
watch: {
|
||||
"input.from"(newVal) {
|
||||
if(newVal == 'PEM'){
|
||||
this.convert_to = ['PKCS12','PKCS7','DER','JKS'];
|
||||
}else if(newVal == 'PKCS12'){
|
||||
this.convert_to = ['PEM','JKS'];
|
||||
}else if(newVal == 'JKS'){
|
||||
this.convert_to = ['PEM','PKCS12'];
|
||||
}else if(newVal == 'DER'){
|
||||
this.convert_to = ['PEM'];
|
||||
}else if(newVal == 'PKCS7'){
|
||||
this.convert_to = ['PEM'];
|
||||
}else{
|
||||
this.convert_to = [];
|
||||
}
|
||||
this.input.to = ''
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
handleFile(name, event) {
|
||||
this.input[name] = event.target.files[0];
|
||||
},
|
||||
submit() {
|
||||
if(this.input.from == ''){
|
||||
layer.alert('转换源格式不能为空');return;
|
||||
}
|
||||
if(this.input.to == ''){
|
||||
layer.alert('转换目标格式不能为空');return;
|
||||
}
|
||||
var data = new FormData();
|
||||
data.append('from', this.input.from);
|
||||
data.append('to', this.input.to);
|
||||
if(this.input.from == 'PEM' && this.input.to != 'DER'){
|
||||
if(this.input.to != 'PKCS7'){
|
||||
if(this.input.pem_private == null){layer.alert('PEM私钥不能为空');return;}
|
||||
data.append('private', this.input.pem_private);
|
||||
}
|
||||
if(this.input.pem_mycert == null){layer.alert('PEM服务器证书不能为空');return;}
|
||||
data.append('cert', this.input.pem_mycert);
|
||||
data.append('cacert', this.input.pem_cacert);
|
||||
if(this.input.to == 'PKCS12'){
|
||||
data.append('pass', this.input.pkcs12_pass);
|
||||
}else if(this.input.to == 'JKS'){
|
||||
if(this.input.jks_pass == ''){layer.alert('JKS证书密码不能为空');return;}
|
||||
data.append('pass', this.input.jks_pass);
|
||||
}
|
||||
}else if(this.input.from == 'PEM' && this.input.to == 'DER'){
|
||||
if(this.input.pem_cert == null){layer.alert('PEM证书不能为空');return;}
|
||||
data.append('cert', this.input.pem_cert);
|
||||
}else if(this.input.from == 'DER' && this.input.to == 'PEM'){
|
||||
if(this.input.der_cert == null){layer.alert('DER证书不能为空');return;}
|
||||
data.append('cert', this.input.der_cert);
|
||||
}else if(this.input.from == 'PKCS12'){
|
||||
if(this.input.pkcs12_cert == null){layer.alert('PKCS12证书文件不能为空');return;}
|
||||
data.append('cert', this.input.pkcs12_cert);
|
||||
if(this.input.to == 'PEM'){
|
||||
data.append('pass', this.input.pkcs12_pass);
|
||||
}else if(this.input.to == 'JKS'){
|
||||
if(this.input.jks_pass == ''){layer.alert('新JKS证书密码不能为空');return;}
|
||||
data.append('srcpass', this.input.pkcs12_pass);
|
||||
data.append('destpass', this.input.jks_pass);
|
||||
}
|
||||
}else if(this.input.from == 'JKS'){
|
||||
if(this.input.jks_cert == null){layer.alert('JKS证书文件不能为空');return;}
|
||||
data.append('cert', this.input.jks_cert);
|
||||
if(this.input.to == 'PEM'){
|
||||
data.append('pass', this.input.jks_pass);
|
||||
}else if(this.input.to == 'PKCS12'){
|
||||
if(this.input.jks_pass == ''){layer.alert('原JKS证书密码不能为空');return;}
|
||||
data.append('srcpass', this.input.jks_pass);
|
||||
data.append('destpass', this.input.pkcs12_pass);
|
||||
}
|
||||
}else if(this.input.from == 'PKCS7' && this.input.to == 'PEM'){
|
||||
if(this.input.pkcs7_cert == null){layer.alert('PKCS7证书不能为空');return;}
|
||||
data.append('cert', this.input.pkcs7_cert);
|
||||
}
|
||||
this.show_result = false;
|
||||
var ii = layer.load();
|
||||
var that = this;
|
||||
$.ajax({
|
||||
type : "POST",
|
||||
url : "/api/{$plugin.alias}/convert",
|
||||
data : data,
|
||||
processData: false,
|
||||
contentType: false,
|
||||
dataType : 'json',
|
||||
success : function(data) {
|
||||
layer.close(ii);
|
||||
if(data.status == 'ok'){
|
||||
that.result_file = data.data.files;
|
||||
that.show_result = true;
|
||||
}else{
|
||||
layer.alert(data.message, {icon: 7});
|
||||
}
|
||||
},
|
||||
error : function(){
|
||||
layer.close(ii);
|
||||
layer.msg('服务器错误', {icon: 5});
|
||||
}
|
||||
});
|
||||
},
|
||||
download(index) {
|
||||
var file_info = this.result_file[index]
|
||||
if(!file_info) return;
|
||||
var byteCharacters = window.atob(file_info.data)
|
||||
var byteNumbers = new Array(byteCharacters.length)
|
||||
for (var i = 0; i < byteCharacters.length; i++) {
|
||||
byteNumbers[i] = byteCharacters.charCodeAt(i)
|
||||
}
|
||||
var byteArray = new Uint8Array(byteNumbers)
|
||||
var blob = new Blob([byteArray], {type: "application/force-download"});
|
||||
saveAs(blob, file_info.file);
|
||||
}
|
||||
},
|
||||
})
|
||||
</script>
|
||||
{/block}
|
||||
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
/**
|
||||
* CSR生成与查看
|
||||
*/
|
||||
|
||||
namespace plugin\dev\csr;
|
||||
|
||||
use app\Plugin;
|
||||
use Exception;
|
||||
|
||||
error_reporting(0);
|
||||
|
||||
class App extends Plugin
|
||||
{
|
||||
|
||||
private static $key_type = ['RSA', 'DSA', 'DH', 'EC'];
|
||||
|
||||
public function index()
|
||||
{
|
||||
return $this->view();
|
||||
}
|
||||
|
||||
public function query(){
|
||||
$csr = input('post.csr');
|
||||
if(!$csr) return msg('error','no csr');
|
||||
|
||||
$subject_info = openssl_csr_get_subject($csr);
|
||||
if(!$subject_info){
|
||||
return msg('error', 'CSR文件错误');
|
||||
}
|
||||
|
||||
$pkey_detail = openssl_pkey_get_details(openssl_csr_get_public_key($csr));
|
||||
|
||||
$info = [
|
||||
'通用名称(CN)' => $this->format($subject_info['CN']),
|
||||
'国家(C)' => $this->format($subject_info['C']),
|
||||
'省份(S)' => $this->format($subject_info['ST']),
|
||||
'城市(L)' => $this->format($subject_info['L']),
|
||||
'组织(O)' => $this->format($subject_info['O']),
|
||||
'部门(OU)' => $this->format($subject_info['OU']),
|
||||
'密钥类型' => isset(self::$key_type[$pkey_detail['type']]) ? self::$key_type[$pkey_detail['type']] : '未知',
|
||||
'密钥强度' => $pkey_detail['bits'],
|
||||
];
|
||||
|
||||
return msg('ok','success',['info'=>$info]);
|
||||
}
|
||||
|
||||
private function format($mixed){
|
||||
if(is_array($mixed)){
|
||||
return $mixed[array_key_first($mixed)];
|
||||
}
|
||||
return $mixed;
|
||||
}
|
||||
|
||||
public function generate(){
|
||||
$commonName = input('post.commonName', null, 'trim');//通用名
|
||||
$countryName = input('post.countryName', null, 'trim');//国家
|
||||
$stateOrProvinceName = input('post.stateOrProvinceName', null, 'trim');//省份
|
||||
$localityName = input('post.localityName', null, 'trim');//城市
|
||||
$organizationName = input('post.organizationName', null, 'trim');//组织
|
||||
$organizationalUnitName = input('post.organizationalUnitName', null, 'trim');//部门
|
||||
$subjectAltName = input('post.subjectAltName', null, 'trim');//备用名
|
||||
$emailAddress = input('post.emailAddress', null, 'trim');//邮箱
|
||||
|
||||
$private_key_type = input('post.private_key_type/d');//密钥算法
|
||||
$private_key_bits = input('post.private_key_bits/d');//密钥长度
|
||||
$curve_name = input('post.curve_name');//曲线名称
|
||||
$digest_alg = input('post.digest_alg');//签名算法
|
||||
$pass_phrase = input('post.pass_phrase', null, 'trim');//私钥密码
|
||||
|
||||
//生成公私钥
|
||||
$config = [
|
||||
"private_key_bits" => $private_key_bits,
|
||||
"private_key_type" => $private_key_type,
|
||||
"curve_name" => $curve_name
|
||||
];
|
||||
$privkey = openssl_pkey_new($config);
|
||||
if(!$privkey){
|
||||
return msg('error', '私钥生成失败'.openssl_error_string());
|
||||
}
|
||||
$privateKey = '';
|
||||
openssl_pkey_export($privkey, $privateKey, $pass_phrase?$pass_phrase:null, $config);
|
||||
$pubKey = openssl_pkey_get_details($privkey);
|
||||
$publicKey = $pubKey['key'];
|
||||
|
||||
//生成CSR
|
||||
$dn = array(
|
||||
"countryName" => $countryName,
|
||||
"stateOrProvinceName" => $stateOrProvinceName,
|
||||
"localityName" => $localityName,
|
||||
"organizationName" => $organizationName,
|
||||
"organizationalUnitName" => $organizationalUnitName,
|
||||
"commonName" => $commonName,
|
||||
"emailAddress" => $emailAddress,
|
||||
"subjectAltName" => $subjectAltName
|
||||
);
|
||||
$dn = array_filter($dn);
|
||||
$csr = openssl_csr_new($dn, $privkey, array('digest_alg' => $digest_alg));
|
||||
if(!$csr){
|
||||
return msg('error', 'CSR生成失败'.openssl_error_string());
|
||||
}
|
||||
openssl_csr_export($csr, $csrout);
|
||||
|
||||
return msg('ok', 'success', ['public_key'=>$publicKey, 'private_key'=>$privateKey, 'csr'=>$csrout]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,382 @@
|
||||
{extend name="common/plugin_layout" /}
|
||||
{block name="title"}{$plugin.title} - {:config_get('title')}{/block}
|
||||
{block name="main"}
|
||||
<style>
|
||||
.query-title{text-align: right; }
|
||||
@media (min-width: 576px) { .query-title{width: 150px;} }
|
||||
</style>
|
||||
<div class="container-xl" id="app">
|
||||
<div class="col-sm-12 col-md-10 col-xl-8 center-block">
|
||||
<div class="card card-preview">
|
||||
<div class="card-inner mt-3">
|
||||
<div class="nya-title nk-ibx-action-item progress-rating">
|
||||
<span class="nk-menu-text font-weight-bold">CSR生成与查看</span>
|
||||
</div>
|
||||
<ul class="nav nav-tabs">
|
||||
<li class="nav-item"><a class="nav-link" @click="show=0" :class="{'active':show===0}" href="javascript:">CSR查看</a></li>
|
||||
<li class="nav-item"><a class="nav-link" @click="show=1" :class="{'active':show===1}" href="javascript:">CSR生成</a></li>
|
||||
</ul>
|
||||
<div class="tab-content">
|
||||
<div class="tab-pane" id="tabItem1" :class="{'active':show===0}">
|
||||
<div class="form-group mt-3">
|
||||
<div class="form-control-wrap">
|
||||
<div class="custom-control custom-radio mr-3">
|
||||
<input type="radio" class="custom-control-input" v-model="inputtype" id="customRadio1" :value="0">
|
||||
<label class="custom-control-label" for="customRadio1">粘贴文件内容</label>
|
||||
</div>
|
||||
<div class="custom-control custom-radio mr-3">
|
||||
<input type="radio" class="custom-control-input" v-model="inputtype" id="customRadio2" :value="1">
|
||||
<label class="custom-control-label" for="customRadio2">上传文件</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group" v-show="inputtype==0">
|
||||
<label class="form-label">CSR请求文件:</label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" rows="6" v-model="csr" placeholder="-----BEGIN CERTIFICATE REQUEST-----"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group" v-show="inputtype==1">
|
||||
<label class="form-label">CSR请求文件:</label>
|
||||
<div class="form-control-wrap">
|
||||
<div class="custom-file">
|
||||
<input type="file" class="custom-file-input" id="customFile1" accept=".csr" required @change="handleFile">
|
||||
<label class="custom-file-label" for="customFile1">点击选择文件</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<button class="btn btn-dim btn-outline-primary btn-block card-link mb-3" @click="query">
|
||||
查看CSR信息
|
||||
</button>
|
||||
</div>
|
||||
<div class="tab-pane" id="tabItem2" :class="{'active':show===1}">
|
||||
<h6 class="text-info">主题信息(以下均为选填)</h6>
|
||||
<div class="row">
|
||||
<div class="form-group col-md-4 col-6">
|
||||
<label class="form-label">通用名(CN)</label>
|
||||
<div class="form-control-wrap">
|
||||
<input type="text" v-model="input.commonName" placeholder="站点的域名" class="form-control">
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group col-md-4 col-6">
|
||||
<label class="form-label">组织(O)</label>
|
||||
<div class="form-control-wrap">
|
||||
<input type="text" v-model="input.organizationName" placeholder="机构名称,例如:Tencent" class="form-control">
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group col-md-4 col-6">
|
||||
<label class="form-label">部门(OU)</label>
|
||||
<div class="form-control-wrap">
|
||||
<input type="text" v-model="input.organizationalUnitName" placeholder="部门名称" class="form-control">
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group col-md-4 col-6">
|
||||
<label class="form-label">国家(C)</label>
|
||||
<div class="form-control-wrap">
|
||||
<input type="text" v-model="input.countryName" placeholder="国家简称,例如:CN" class="form-control">
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group col-md-4 col-6">
|
||||
<label class="form-label">省份(S)</label>
|
||||
<div class="form-control-wrap">
|
||||
<input type="text" v-model="input.stateOrProvinceName" placeholder="省级地区,例如:Guangzhou" class="form-control">
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group col-md-4 col-6">
|
||||
<label class="form-label">城市(L)</label>
|
||||
<div class="form-control-wrap">
|
||||
<input type="text" v-model="input.localityName" placeholder="市级地区,例如:Shenzhen" class="form-control">
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group col-md-8 col-6">
|
||||
<label class="form-label">备用名</label>
|
||||
<div class="form-control-wrap">
|
||||
<input type="text" v-model="input.subjectAltName" placeholder="备用域名,多个用,分隔" class="form-control">
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group col-md-4 col-6">
|
||||
<label class="form-label">邮箱</label>
|
||||
<div class="form-control-wrap">
|
||||
<input type="text" v-model="input.emailAddress" placeholder="邮箱" class="form-control">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<h6 class="text-info">密钥信息</h6>
|
||||
<div class="row">
|
||||
<div class="form-group col-6">
|
||||
<label class="form-label">密钥算法</label>
|
||||
<select class="form-control" v-model="input.private_key_type">
|
||||
<option v-for="v,index in private_key_type" :value="index">{{v}}</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group col-6" v-if="input.private_key_type!=3">
|
||||
<label class="form-label">密钥长度</label>
|
||||
<select class="form-control" v-model="input.private_key_bits">
|
||||
<option v-for="v in private_key_bits" :value="v">{{v}} bit</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group col-6" v-if="input.private_key_type==3">
|
||||
<label class="form-label">曲线名称</label>
|
||||
<select class="form-control" v-model="input.curve_name">
|
||||
<option v-for="v in curve_name" :value="v">{{v}}</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group col-6">
|
||||
<label class="form-label">签名算法</label>
|
||||
<select class="form-control" v-model="input.digest_alg">
|
||||
<option v-for="v in digest_alg" :value="v">{{v}}</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group col-6">
|
||||
<label class="form-label">私钥密码</label>
|
||||
<div class="form-control-wrap">
|
||||
<input type="text" v-model="input.pass_phrase" placeholder="可不设置" class="form-control">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<button class="btn btn-dim btn-outline-primary btn-block card-link mb-3" @click="generate">
|
||||
生成CSR文件
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card card-preview" v-show="show==0&&showresult" style="display:none">
|
||||
<div class="card-inner mt-3">
|
||||
<div class="nya-title nk-ibx-action-item progress-rating">
|
||||
<span class="nk-menu-text font-weight-bold">查询结果</span>
|
||||
</div>
|
||||
<div class="table-responsive mb-2">
|
||||
<table class="table table-hover table-bordered">
|
||||
<tbody>
|
||||
<tr v-for="(item,index) in info"><td class="query-title">{{index}}</td><td>{{item}}</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card card-preview" v-show="show==1&&showresult2" style="display:none">
|
||||
<div class="card-inner mt-3">
|
||||
<div class="nya-title nk-ibx-action-item progress-rating">
|
||||
<span class="nk-menu-text font-weight-bold">生成结果</span>
|
||||
</div>
|
||||
<div class="table-responsive mb-2">
|
||||
<div class="form-group">
|
||||
<label class="form-label">CSR(PEM格式) <em class="icon ni ni-download" title="下载保存" v-show="result.csr" @click="download('csr')"></em></label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" rows="6" v-model="result.csr"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<div class="d-flex flex-wrap">
|
||||
<div class="form-group flex-fill mr-2">
|
||||
<label class="form-label">公钥 <em class="icon ni ni-download" title="下载保存" v-show="result.public_key" @click="download('public_key')"></em></label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" rows="8" v-model="result.public_key"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group flex-fill">
|
||||
<label class="form-label">私钥 <em class="icon ni ni-download" title="下载保存" v-show="result.private_key" @click="download('private_key')"></em></label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" rows="8" v-model="result.private_key"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">公私钥格式</label>
|
||||
<select class="form-control" v-model="result.key_type">
|
||||
<option value="pem">PEM格式</option>
|
||||
<option value="base64">Base64无换行</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{/block}
|
||||
{block name="script"}
|
||||
<script src="{$cdn_cdnjs}vue/2.6.14/vue.min.js"></script>
|
||||
<script src="{$cdn_cdnjs}FileSaver.js/2.0.5/FileSaver.min.js"></script>
|
||||
<script>
|
||||
function pemToBase64(pem) {
|
||||
var lines = pem.split('\n');
|
||||
var encoded = '';
|
||||
for (var i = 0; i < lines.length; i++) {
|
||||
if (lines[i].trim().length > 0 && lines[i].indexOf('-----BEGIN') === -1 && lines[i].indexOf('-----END') === -1) {
|
||||
encoded += lines[i].trim();
|
||||
}
|
||||
}
|
||||
return encoded;
|
||||
}
|
||||
function base64ToPem(base64Key, type) {
|
||||
var pemHeader = '-----BEGIN ' + type + '-----\n';
|
||||
var pemFooter = '\n-----END ' + type + '-----\n';
|
||||
var chunkSize = 64;
|
||||
var base64KeyWithBreaks = base64Key.match(new RegExp('.{1,' + chunkSize + '}', 'g')).join('\n');
|
||||
var pemKey = pemHeader + base64KeyWithBreaks + pemFooter;
|
||||
return pemKey;
|
||||
}
|
||||
new Vue({
|
||||
el: '#app',
|
||||
data: {
|
||||
show: 0,
|
||||
inputtype: 0,
|
||||
csr: '',
|
||||
showresult: false,
|
||||
showresult2: false,
|
||||
info: [],
|
||||
private_key_type: [
|
||||
'RSA',
|
||||
'DSA',
|
||||
'DH',
|
||||
'ECC'
|
||||
],
|
||||
private_key_bits: [512,1024,2048,4096],
|
||||
curve_name: [
|
||||
'prime256v1',
|
||||
'secp256k1',
|
||||
'secp384r1',
|
||||
'secp521r1',
|
||||
],
|
||||
digest_alg: [
|
||||
'md5',
|
||||
'sha1',
|
||||
'sha224',
|
||||
'sha256',
|
||||
'sha384',
|
||||
'sha512',
|
||||
],
|
||||
input: {
|
||||
commonName: '',
|
||||
countryName: '',
|
||||
stateOrProvinceName: '',
|
||||
localityName: '',
|
||||
organizationName: '',
|
||||
organizationalUnitName: '',
|
||||
subjectAltName: '',
|
||||
emailAddress: '',
|
||||
private_key_type: 0,
|
||||
private_key_bits: 2048,
|
||||
curve_name: 'prime256v1',
|
||||
digest_alg: 'sha256',
|
||||
pass_phrase: '',
|
||||
},
|
||||
result:{
|
||||
csr: '',
|
||||
key_type: 'pem',
|
||||
public_key: '',
|
||||
private_key: '',
|
||||
}
|
||||
},
|
||||
watch: {
|
||||
"input.private_key_type"(newVal) {
|
||||
if(newVal == 2){
|
||||
this.private_key_bits = [512,1024,2048]
|
||||
}else{
|
||||
this.private_key_bits = [512,1024,2048,4096]
|
||||
}
|
||||
},
|
||||
"result.key_type"(newVal) {
|
||||
if(this.result.public_key != ''){
|
||||
if(newVal == 'pem'){
|
||||
this.result.public_key = base64ToPem(this.result.public_key, 'PUBLIC KEY')
|
||||
}else{
|
||||
this.result.public_key = pemToBase64(this.result.public_key)
|
||||
}
|
||||
}
|
||||
if(this.result.private_key != ''){
|
||||
if(newVal == 'pem'){
|
||||
this.result.private_key = base64ToPem(this.result.private_key, this.input.pass_phrase ? 'ENCRYPTED PRIVATE KEY' : 'PRIVATE KEY')
|
||||
}else{
|
||||
this.result.private_key = pemToBase64(this.result.private_key)
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
methods: {
|
||||
handleFile(event) {
|
||||
var file = event.target.files[0];
|
||||
layer.load(0, {shade:0.1});
|
||||
var that = this;
|
||||
that.csr = '';
|
||||
var reader = new FileReader();
|
||||
reader.onload = () => {
|
||||
layer.closeAll();
|
||||
var csr = reader.result;
|
||||
if(csr.indexOf('BEGIN CERTIFICATE REQUEST') == -1){
|
||||
layer.alert('证书文件错误', {icon: 5});return;
|
||||
}
|
||||
that.csr = csr;
|
||||
};
|
||||
reader.onerror = () => {
|
||||
layer.closeAll();
|
||||
layer.alert('证书文件读取失败', {icon: 5});return;
|
||||
};
|
||||
reader.readAsText(file);
|
||||
},
|
||||
query() {
|
||||
if(this.csr == ''){
|
||||
layer.alert('证书内容不能为空');return;
|
||||
}
|
||||
layer.load(0, {shade:0.1});
|
||||
var that=this;
|
||||
$.ajax({
|
||||
url: '/api/{$plugin.alias}/query',
|
||||
type: 'post',
|
||||
dataType: 'json',
|
||||
data: {csr: that.csr},
|
||||
cache: false,
|
||||
success: function (data) {
|
||||
layer.closeAll();
|
||||
if(data.status=='ok'){
|
||||
that.info = data.data.info;
|
||||
that.showresult = true;
|
||||
}else{
|
||||
layer.alert(data.message, {icon: 5});
|
||||
}
|
||||
},
|
||||
error: function () {
|
||||
layer.closeAll();
|
||||
layer.msg('服务器错误', {icon: 5});
|
||||
}
|
||||
});
|
||||
},
|
||||
generate() {
|
||||
layer.load(0, {shade:0.1});
|
||||
var that=this;
|
||||
$.ajax({
|
||||
url: '/api/{$plugin.alias}/generate',
|
||||
type: 'post',
|
||||
dataType: 'json',
|
||||
data: {...that.input},
|
||||
cache: false,
|
||||
success: function (data) {
|
||||
layer.closeAll();
|
||||
if(data.status=='ok'){
|
||||
that.result.csr = data.data.csr;
|
||||
that.result.public_key = data.data.public_key;
|
||||
that.result.private_key = data.data.private_key;
|
||||
that.showresult2 = true;
|
||||
}else{
|
||||
layer.alert(data.message, {icon: 5});
|
||||
}
|
||||
},
|
||||
error: function () {
|
||||
layer.closeAll();
|
||||
layer.msg('服务器错误', {icon: 5});
|
||||
}
|
||||
});
|
||||
},
|
||||
download(name) {
|
||||
var content = this.result[name]
|
||||
if(!content) return;
|
||||
var ext = this.result.key_type == 'base64' ? '.txt' : '.pem';
|
||||
if(name == 'csr') ext = '.csr';
|
||||
var filename = name + ext
|
||||
var blob = new Blob([content], {type: "application/force-download"});
|
||||
saveAs(blob, filename);
|
||||
}
|
||||
},
|
||||
})
|
||||
</script>
|
||||
{/block}
|
||||
+105
-51
@@ -11,45 +11,45 @@
|
||||
<div class="form-group">
|
||||
<label class="form-label" for="input">字符串</label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" id="input" rows="8" placeholder="需要加密/解密的字符串"></textarea>
|
||||
<textarea class="form-control" id="input" rows="8" placeholder="需要加密/解密的字符串" v-model="set.input"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">加密方式</label>
|
||||
<div class="form-control-wrap">
|
||||
<div class="custom-control custom-radio m-1"><input type="radio" class="custom-control-input" name="encrypt_type" id="customRadio1" value="AES" checked><label class="custom-control-label" for="customRadio1">AES</label></div>
|
||||
<div class="custom-control custom-radio m-1"><input type="radio" class="custom-control-input" name="encrypt_type" id="customRadio2" value="DES"><label class="custom-control-label" for="customRadio2">DES</label></div>
|
||||
<div class="custom-control custom-radio m-1"><input type="radio" class="custom-control-input" name="encrypt_type" id="customRadio3" value="RC4"><label class="custom-control-label" for="customRadio3">RC4</label></div>
|
||||
<div class="custom-control custom-radio m-1"><input type="radio" class="custom-control-input" name="encrypt_type" id="customRadio4" value="Rabbit"><label class="custom-control-label" for="customRadio4">Rabbit</label></div>
|
||||
<div class="custom-control custom-radio m-1"><input type="radio" class="custom-control-input" name="encrypt_type" id="customRadio5" value="TripleDES"><label class="custom-control-label" for="customRadio5">TripleDES</label></div>
|
||||
<div class="custom-control custom-radio m-1"><input type="radio" class="custom-control-input" name="encrypt_type" id="customRadio6" value="RC4Drop"><label class="custom-control-label" for="customRadio6">RC4Drop</label></div>
|
||||
<div class="custom-control custom-radio m-1"><input type="radio" class="custom-control-input" name="encrypt_type" id="customRadio7" value="RabbitLegacy"><label class="custom-control-label" for="customRadio7">RabbitLegacy</label></div>
|
||||
<div class="custom-control custom-radio m-1" v-for="(value,index) in encrypt_types"><input type="radio" class="custom-control-input" name="encrypt_type" :id="'customRadio'+index" :value="value" v-model="encrypt_type"><label class="custom-control-label" :for="'customRadio'+index">{{value}}</label></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">密钥</label>
|
||||
<div class="form-control-wrap">
|
||||
<input type="text" id="passphrase" placeholder="输入加密/解密的密钥" class="form-control">
|
||||
<input type="text" v-model="set.passphrase" placeholder="输入加密/解密的密钥" class="form-control">
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group" v-show="show_iv">
|
||||
<label class="form-label">IV</label>
|
||||
<div class="form-control-wrap">
|
||||
<input type="text" v-model="set.iv" placeholder="输入IV值" class="form-control">
|
||||
</div>
|
||||
</div>
|
||||
<div class="row pt-1 pb-1">
|
||||
<div class="col-3">
|
||||
<button class="btn btn-dim btn-outline-secondary btn-block card-link" onclick="encrypt()">
|
||||
<button class="btn btn-dim btn-outline-secondary btn-block card-link" @click="encrypt">
|
||||
<em class="icon ni ni-lock"></em>加密
|
||||
</button>
|
||||
</div>
|
||||
<div class="col-3">
|
||||
<button class="btn btn-dim btn-outline-secondary btn-block card-link" onclick="decrypt()">
|
||||
<button class="btn btn-dim btn-outline-secondary btn-block card-link" @click="decrypt">
|
||||
<em class="icon ni ni-unlock"></em>解密
|
||||
</button>
|
||||
</div>
|
||||
<div class="col-3">
|
||||
<button class="btn btn-dim btn-outline-secondary btn-block card-link" onclick="exchange()">
|
||||
<button class="btn btn-dim btn-outline-secondary btn-block card-link" @click="exchange">
|
||||
<em class="icon ni ni-exchange-v"></em>交换
|
||||
</button>
|
||||
</div>
|
||||
<div class="col-3">
|
||||
<button class="btn btn-dim btn-outline-secondary btn-block card-link" onclick="reset()">
|
||||
<button class="btn btn-dim btn-outline-secondary btn-block card-link" @click="reset">
|
||||
<em class="icon ni ni-trash-empty"></em>清空
|
||||
</button>
|
||||
</div>
|
||||
@@ -57,7 +57,7 @@
|
||||
<div class="form-group mt-3">
|
||||
<label class="form-label" for="output">结果</label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" id="output" rows="8" placeholder="加密/解密的结果"></textarea>
|
||||
<textarea class="form-control" v-model="set.output" rows="8" placeholder="加密/解密的结果"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -66,44 +66,98 @@
|
||||
</div>
|
||||
{/block}
|
||||
{block name="script"}
|
||||
<script src="{$cdn_cdnjs}vue/2.6.14/vue.min.js"></script>
|
||||
<script src="{$cdn_cdnjs}crypto-js/4.1.1/crypto-js.min.js"></script>
|
||||
<script src="/assets/sm/sm4.js"></script>
|
||||
<script>
|
||||
function encrypt() {
|
||||
var input = $("#input").val();
|
||||
var passphrase = $("#passphrase").val();
|
||||
var encrypt_type = $("input[name='encrypt_type']").val();
|
||||
if(input == '' || passphrase == '') return;
|
||||
try {
|
||||
var output = CryptoJS[encrypt_type].encrypt(input, passphrase).toString();
|
||||
$("#output").val(output);
|
||||
} catch (e) {
|
||||
layer.msg(e.message, {icon:2});
|
||||
$("#output").val('');
|
||||
}
|
||||
}
|
||||
function decrypt() {
|
||||
var input = $("#input").val();
|
||||
var passphrase = $("#passphrase").val();
|
||||
var encrypt_type = $("input[name='encrypt_type']").val();
|
||||
if(input == '' || passphrase == '') return;
|
||||
try {
|
||||
input = CryptoJS[encrypt_type].decrypt(input, passphrase).toString(CryptoJS.enc.Utf8);
|
||||
$("#output").val(input);
|
||||
} catch (e) {
|
||||
layer.msg(e.message, {icon:2});
|
||||
$("#output").val('');
|
||||
}
|
||||
}
|
||||
function exchange() {
|
||||
var input = $("#input").val();
|
||||
var output = $("#output").val();
|
||||
$("#input").val(output);
|
||||
$("#output").val(input);
|
||||
}
|
||||
function reset() {
|
||||
$("#input").val('');
|
||||
$("#output").val('');
|
||||
$("#passphrase").val('');
|
||||
}
|
||||
new Vue({
|
||||
el: '#app',
|
||||
data: {
|
||||
encrypt_type: 'AES',
|
||||
encrypt_types: ['AES', 'DES', 'RC4', 'Rabbit', 'TripleDES', 'RC4Drop', 'RabbitLegacy', 'SM4-ECB', 'SM4-CBC'],
|
||||
set: {
|
||||
input: '',
|
||||
output: '',
|
||||
passphrase: '',
|
||||
iv: '0123456789abcdef',
|
||||
},
|
||||
show_iv: false
|
||||
},
|
||||
watch: {
|
||||
encrypt_type(newVal) {
|
||||
if(newVal == 'SM4-CBC'){
|
||||
this.show_iv = true;
|
||||
}else{
|
||||
this.show_iv = false;
|
||||
}
|
||||
},
|
||||
},
|
||||
methods: {
|
||||
encrypt() {
|
||||
if(this.set.input == '' || this.set.passphrase == '') return;
|
||||
try {
|
||||
if(this.encrypt_type == 'SM4-ECB'){
|
||||
var key = this.stringToHex(this.set.passphrase);
|
||||
if(key.length != 32){layer.msg('SM4加密密钥必须是16个字符', {icon:0, time:1500});return;}
|
||||
this.set.output = sm4.encrypt(this.set.input, key);
|
||||
return;
|
||||
}else if(this.encrypt_type == 'SM4-CBC'){
|
||||
if(this.set.iv == '') return;
|
||||
var key = this.stringToHex(this.set.passphrase);
|
||||
var iv = this.stringToHex(this.set.iv);
|
||||
if(key.length != 32){layer.msg('SM4加密密钥必须填是16个字符', {icon:0, time:1500});return;}
|
||||
if(iv.length != 32){layer.msg('SM4加密IV必须填是16个字符', {icon:0, time:1500});return;}
|
||||
this.set.output = sm4.encrypt(this.set.input, key, {mode: 'cbc', iv: iv});
|
||||
return;
|
||||
}
|
||||
this.set.output = CryptoJS[`${this.encrypt_type}`].encrypt(this.set.input, this.set.passphrase).toString();
|
||||
} catch (e) {
|
||||
layer.msg(e.message, {icon:2});
|
||||
this.set.output = ''
|
||||
}
|
||||
},
|
||||
decrypt() {
|
||||
if(this.set.input == '' || this.set.passphrase == '') return;
|
||||
try {
|
||||
if(this.encrypt_type == 'SM4-ECB'){
|
||||
var key = this.stringToHex(this.set.passphrase);
|
||||
if(key.length != 32){layer.msg('SM4解密密钥必须是16个字符', {icon:0, time:1500});return;}
|
||||
this.set.output = sm4.decrypt(this.set.input, key);
|
||||
return;
|
||||
}else if(this.encrypt_type == 'SM4-CBC'){
|
||||
if(this.set.iv == '') return;
|
||||
var key = this.set.passphrase;
|
||||
var iv = this.set.iv;
|
||||
if(key.length != 32){layer.msg('SM4解密密钥必须填是16个字符', {icon:0, time:1500});return;}
|
||||
if(iv.length != 32){layer.msg('SM4解密IV必须填是16个字符', {icon:0, time:1500});return;}
|
||||
this.set.output = sm4.decrypt(this.set.input, key, {mode: 'cbc', iv: iv});
|
||||
return;
|
||||
}
|
||||
this.set.input = CryptoJS[`${this.encrypt_type}`].decrypt(this.set.output, this.set.passphrase).toString(CryptoJS.enc.Utf8);
|
||||
} catch (e) {
|
||||
layer.msg(e.message, {icon:2});
|
||||
this.set.output = ''
|
||||
}
|
||||
},
|
||||
reset() {
|
||||
this.set.input = ''
|
||||
this.set.output = ''
|
||||
this.set.passphrase = ''
|
||||
},
|
||||
exchange() {
|
||||
var input = this.set.input;
|
||||
var output = this.set.output;
|
||||
this.set.input = output;
|
||||
this.set.output = input;
|
||||
},
|
||||
stringToHex(str){
|
||||
var val="";
|
||||
for(var i = 0; i < str.length; i++){
|
||||
val += str.charCodeAt(i).toString(16);
|
||||
}
|
||||
return val;
|
||||
}
|
||||
},
|
||||
})
|
||||
</script>
|
||||
{/block}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
/**
|
||||
* 国密公私钥工具
|
||||
*/
|
||||
|
||||
namespace plugin\dev\guomi;
|
||||
|
||||
use app\Plugin;
|
||||
|
||||
class App extends Plugin
|
||||
{
|
||||
|
||||
public function index()
|
||||
{
|
||||
return $this->view();
|
||||
}
|
||||
|
||||
public function output()
|
||||
{
|
||||
$type = input('post.type/d');
|
||||
if($type == 1){
|
||||
$cert = input('post.cert');
|
||||
$pkey_res = openssl_pkey_get_public($cert);
|
||||
if(!$pkey_res){
|
||||
return msg('error', '导出失败,公钥证书不正确');
|
||||
}
|
||||
$pkey_detail = openssl_pkey_get_details($pkey_res);
|
||||
$pkey_hex = bin2hex($this->pem2der($pkey_detail['key']));
|
||||
if(strlen($pkey_hex) > 130) $pkey_hex = substr($pkey_hex, -130);
|
||||
return msg('ok', '导出成功', $pkey_hex);
|
||||
}elseif($type == 2){
|
||||
$csr = input('post.csr');
|
||||
$pkey_res = openssl_csr_get_public_key($csr);
|
||||
if(!$pkey_res){
|
||||
return msg('error', '导出失败,CSR不正确');
|
||||
}
|
||||
$pkey_detail = openssl_pkey_get_details($pkey_res);
|
||||
$pkey_hex = bin2hex($this->pem2der($pkey_detail['key']));
|
||||
if(strlen($pkey_hex) > 130) $pkey_hex = substr($pkey_hex, -130);
|
||||
return msg('ok', '导出成功', $pkey_hex);
|
||||
}
|
||||
}
|
||||
|
||||
private function pem2der($pem_data)
|
||||
{
|
||||
$begin = "-----";
|
||||
$end = "-----END";
|
||||
$pem_data = substr($pem_data, strpos($pem_data, $begin, 6) + strlen($begin));
|
||||
$pem_data = substr($pem_data, 0, strpos($pem_data, $end));
|
||||
$der = base64_decode($pem_data);
|
||||
return $der;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,478 @@
|
||||
{extend name="common/plugin_layout" /}
|
||||
{block name="title"}{$plugin.title} - {:config_get('title')}{/block}
|
||||
{block name="main"}
|
||||
<div class="container-xl" id="app">
|
||||
<div class="col-sm-12 col-md-10 col-xl-8 center-block">
|
||||
<div class="card card-preview">
|
||||
<div class="card-inner mt-3">
|
||||
<div class="nya-title nk-ibx-action-item progress-rating">
|
||||
<span class="nk-menu-text font-weight-bold">国密SM2公私钥生成、签名、加解密</span>
|
||||
</div>
|
||||
<ul class="nav nav-tabs">
|
||||
<li class="nav-item"><a class="nav-link" @click="show=0" :class="{'active':show===0}" href="javascript:">公私钥生成</a></li>
|
||||
<li class="nav-item"><a class="nav-link" @click="show=1" :class="{'active':show===1}" href="javascript:">加密解密</a></li>
|
||||
<li class="nav-item"><a class="nav-link" @click="show=2" :class="{'active':show===2}" href="javascript:">签名验签</a></li>
|
||||
<li class="nav-item"><a class="nav-link" @click="show=3" :class="{'active':show===3}" href="javascript:">公私钥对验证</a></li>
|
||||
<li class="nav-item"><a class="nav-link" @click="show=4" :class="{'active':show===4}" href="javascript:">公钥导出</a></li>
|
||||
<li class="nav-item"><a class="nav-link" @click="show=5" :class="{'active':show===5}" href="javascript:">公钥压缩</a></li>
|
||||
</ul>
|
||||
<div class="tab-content">
|
||||
<div class="tab-pane" id="tabItem1" :class="{'active':show===0}">
|
||||
<button class="btn btn-dim btn-outline-secondary btn-block card-link mb-3" @click="generate">
|
||||
生成
|
||||
</button>
|
||||
<div class="d-flex flex-wrap">
|
||||
<div class="form-group flex-fill mr-2">
|
||||
<label class="form-label">SM2公钥 <em class="icon ni ni-download" title="下载保存" v-show="mutipart_from.public_key" @click="download('public_key')"></em></label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" rows="6" v-model="mutipart_from.public_key"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group flex-fill">
|
||||
<label class="form-label">SM2私钥 <em class="icon ni ni-download" title="下载保存" v-show="mutipart_from.private_key" @click="download('private_key')"></em></label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" rows="6" v-model="mutipart_from.private_key"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">密钥数据类型</label>
|
||||
<select class="form-control" v-model="mutipart_from.key_type">
|
||||
<option value="hex">Hex(十六进制)</option>
|
||||
<option value="base64">Base64</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<div class="tab-pane" id="tabItem1" :class="{'active':show===1}">
|
||||
<div class="form-group">
|
||||
<label class="form-label">密文数据顺序</label>
|
||||
<select class="form-control" v-model="crypto_from.type">
|
||||
<option :value="1">新标准(C1C3C2)</option>
|
||||
<option :value="0">老标准(C1C2C3)</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">密文数据类型</label>
|
||||
<select class="form-control" v-model="crypto_from.data_type">
|
||||
<option value="hex">Hex(十六进制)</option>
|
||||
<option value="base64">Base64</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="row pt-1 pb-1 mb-3">
|
||||
<div class="col-6">
|
||||
<button class="btn btn-dim btn-outline-secondary btn-block card-link" @click="encrypt">
|
||||
加密
|
||||
</button>
|
||||
</div>
|
||||
<div class="col-6">
|
||||
<button class="btn btn-dim btn-outline-secondary btn-block card-link" @click="decrypt">
|
||||
解密
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">原文,未加密的内容</label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" rows="6" v-model="crypto_from.origin"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">密文,加密后的内容</label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" rows="6" v-model="crypto_from.coded"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="tab-pane" id="tabItem1" :class="{'active':show===2}">
|
||||
<div class="form-group">
|
||||
<label class="form-label">是否SM3计算Hash</label>
|
||||
<select class="form-control" v-model="sign_form.hash">
|
||||
<option :value="1">是</option>
|
||||
<option :value="0">否</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">签名结果类型</label>
|
||||
<select class="form-control" v-model="sign_form.type">
|
||||
<option :value="1">ASN1(R,S)</option>
|
||||
<option :value="0">R+S</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">签名数据类型</label>
|
||||
<select class="form-control" v-model="sign_form.data_type">
|
||||
<option value="hex">Hex(十六进制)</option>
|
||||
<option value="base64">Base64</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">UserId</label>
|
||||
<div class="form-control-wrap">
|
||||
<input type="text" v-model="sign_form.userid" class="form-control">
|
||||
</div>
|
||||
</div>
|
||||
<div class="row pt-1 pb-1 mb-3">
|
||||
<div class="col-6">
|
||||
<button class="btn btn-dim btn-outline-secondary btn-block card-link" @click="sign">
|
||||
签名
|
||||
</button>
|
||||
</div>
|
||||
<div class="col-6">
|
||||
<button class="btn btn-dim btn-outline-secondary btn-block card-link" @click="verify">
|
||||
验签
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">待签名,验签原始内容</label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" rows="6" v-model="sign_form.data"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">签名,已签名内容</label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" rows="3" v-model="sign_form.sign"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="tab-pane" id="tabItem1" :class="{'active':show===3}">
|
||||
<div class="d-flex flex-wrap">
|
||||
<div class="form-group flex-fill mr-2">
|
||||
<label class="form-label">SM2公钥</label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" rows="6" v-model="mutipart_from.public_key"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group flex-fill">
|
||||
<label class="form-label">SM2私钥</label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" rows="6" v-model="mutipart_from.private_key"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">密钥数据类型</label>
|
||||
<select class="form-control" v-model="mutipart_from.key_type">
|
||||
<option value="hex">Hex(十六进制)</option>
|
||||
<option value="base64">Base64</option>
|
||||
</select>
|
||||
</div>
|
||||
<button class="btn btn-dim btn-outline-secondary btn-block card-link mb-3" @click="check">
|
||||
验证
|
||||
</button>
|
||||
</div>
|
||||
<div class="tab-pane" id="tabItem1" :class="{'active':show===4}">
|
||||
<div class="form-group">
|
||||
<label class="form-label">导出方式</label>
|
||||
<select class="form-control" v-model="convert_from.type">
|
||||
<option :value="0">从私钥导出公钥</option>
|
||||
<option :value="1">从公钥证书导出公钥</option>
|
||||
<option :value="2">从CSR导出公钥</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="d-flex flex-wrap">
|
||||
<div class="form-group flex-fill mr-2">
|
||||
<label class="form-label">SM2公钥</label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" rows="6" v-model="mutipart_from.public_key" placeholder="导出结果"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group flex-fill" v-show="convert_from.type==0">
|
||||
<label class="form-label">SM2私钥</label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" rows="6" v-model="mutipart_from.private_key"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group flex-fill" v-show="convert_from.type==1">
|
||||
<label class="form-label">SM2公钥证书</label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" rows="8" v-model="convert_from.cert" placeholder="-----BEGIN CERTIFICATE-----"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group flex-fill" v-show="convert_from.type==2">
|
||||
<label class="form-label">CSR</label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" rows="8" v-model="convert_from.csr" placeholder="-----BEGIN CERTIFICATE REQUEST-----"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">密钥数据类型</label>
|
||||
<select class="form-control" v-model="mutipart_from.key_type">
|
||||
<option value="hex">Hex(十六进制)</option>
|
||||
<option value="base64">Base64</option>
|
||||
</select>
|
||||
</div>
|
||||
<button class="btn btn-dim btn-outline-secondary btn-block card-link mb-3" @click="output">
|
||||
导出
|
||||
</button>
|
||||
</div>
|
||||
<div class="tab-pane" id="tabItem1" :class="{'active':show===5}">
|
||||
<div class="form-group">
|
||||
<label class="form-label">SM2公钥</label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" rows="6" v-model="mutipart_from.public_key"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">密钥数据类型</label>
|
||||
<select class="form-control" v-model="mutipart_from.key_type">
|
||||
<option value="hex">Hex(十六进制)</option>
|
||||
<option value="base64">Base64</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="row pt-1 pb-1 mb-3">
|
||||
<div class="col-6">
|
||||
<button class="btn btn-dim btn-outline-secondary btn-block card-link" @click="key_compress">
|
||||
压缩
|
||||
</button>
|
||||
</div>
|
||||
<div class="col-6">
|
||||
<button class="btn btn-dim btn-outline-secondary btn-block card-link" @click="key_uncompress">
|
||||
还原
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{/block}
|
||||
{block name="script"}
|
||||
<script src="{$cdn_cdnjs}vue/2.6.14/vue.min.js"></script>
|
||||
<script src="{$cdn_cdnjs}FileSaver.js/2.0.5/FileSaver.min.js"></script>
|
||||
<script src="/assets/sm/sm2.js"></script>
|
||||
<script src="/assets/sm/sm2_utils.js"></script>
|
||||
<script>
|
||||
new Vue({
|
||||
el: '#app',
|
||||
data: {
|
||||
show: 0,
|
||||
mutipart_from: JSON.parse(localStorage.getItem('{$plugin.alias}_mutipart_from')) || {
|
||||
public_key: '',
|
||||
private_key: '',
|
||||
key_type: 'hex'
|
||||
},
|
||||
crypto_from: {
|
||||
type: 1,
|
||||
data_type: 'hex',
|
||||
origin: '',
|
||||
coded: ''
|
||||
},
|
||||
sign_form: {
|
||||
hash: 1,
|
||||
type: 1,
|
||||
data_type: 'hex',
|
||||
data: '',
|
||||
sign: '',
|
||||
userid: '1234567812345678'
|
||||
},
|
||||
convert_from: {
|
||||
type: 0,
|
||||
cert: '',
|
||||
csr: ''
|
||||
},
|
||||
},
|
||||
watch: {
|
||||
mutipart_from: {
|
||||
handler: (newVal) => {
|
||||
localStorage.setItem('{$plugin.alias}_mutipart_from', JSON.stringify(newVal))
|
||||
},
|
||||
deep: true,
|
||||
},
|
||||
"mutipart_from.key_type"(newVal) {
|
||||
if(this.mutipart_from.public_key != ''){
|
||||
if(newVal == 'hex'){
|
||||
this.mutipart_from.public_key = base64ToHex(this.mutipart_from.public_key)
|
||||
}else{
|
||||
this.mutipart_from.public_key = hexToBase64(this.mutipart_from.public_key)
|
||||
}
|
||||
}
|
||||
if(this.mutipart_from.private_key != ''){
|
||||
if(newVal == 'hex'){
|
||||
this.mutipart_from.private_key = base64ToHex(this.mutipart_from.private_key)
|
||||
}else{
|
||||
this.mutipart_from.private_key = hexToBase64(this.mutipart_from.private_key)
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
generate() {
|
||||
var keypair = sm2.generateKeyPairHex()
|
||||
this.mutipart_from.public_key = keypair.publicKey
|
||||
this.mutipart_from.private_key = keypair.privateKey
|
||||
if(this.mutipart_from.key_type == 'base64'){
|
||||
this.mutipart_from.public_key = hexToBase64(this.mutipart_from.public_key)
|
||||
this.mutipart_from.private_key = hexToBase64(this.mutipart_from.private_key)
|
||||
}
|
||||
},
|
||||
getPublicKey(){
|
||||
if(this.mutipart_from.public_key == '') return;
|
||||
if(this.mutipart_from.key_type == 'base64'){
|
||||
return base64ToHex(this.mutipart_from.public_key)
|
||||
}else{
|
||||
return this.mutipart_from.public_key
|
||||
}
|
||||
},
|
||||
getPrivateKey(){
|
||||
if(this.mutipart_from.private_key == '') return;
|
||||
if(this.mutipart_from.key_type == 'base64'){
|
||||
return base64ToHex(this.mutipart_from.private_key)
|
||||
}else{
|
||||
return this.mutipart_from.private_key
|
||||
}
|
||||
},
|
||||
encrypt() {
|
||||
var that=this;
|
||||
if (this.mutipart_from.public_key == '' || this.mutipart_from.private_key == ''){
|
||||
layer.alert("SM2公钥和私钥不能为空");return
|
||||
}
|
||||
if (this.crypto_from.origin === "") {
|
||||
layer.alert("原文不得为空")
|
||||
return
|
||||
}
|
||||
this.crypto_from.coded = sm2.doEncrypt(this.crypto_from.origin, this.getPublicKey(), this.crypto_from.type);
|
||||
if(this.crypto_from.data_type == 'base64'){
|
||||
this.crypto_from.coded = hexToBase64(this.crypto_from.coded)
|
||||
}
|
||||
},
|
||||
decrypt() {
|
||||
var that=this;
|
||||
if (this.mutipart_from.public_key == '' || this.mutipart_from.private_key == ''){
|
||||
layer.alert("SM2公钥和私钥不能为空");return
|
||||
}
|
||||
if (this.crypto_from.coded === "") {
|
||||
layer.alert("密文不得为空")
|
||||
return
|
||||
}
|
||||
var coded = this.crypto_from.coded;
|
||||
if(this.crypto_from.data_type == 'base64'){
|
||||
coded = base64ToHex(coded)
|
||||
}
|
||||
this.crypto_from.origin = sm2.doDecrypt(coded, this.getPrivateKey(), this.crypto_from.type);
|
||||
},
|
||||
verify() {
|
||||
if (this.mutipart_from.public_key == '' || this.mutipart_from.private_key == ''){
|
||||
layer.alert("SM2公钥和私钥不能为空");return
|
||||
}
|
||||
if (this.sign_form.sign === "") {
|
||||
layer.alert("签名不得为空")
|
||||
return
|
||||
}
|
||||
if (this.sign_form.data === "") {
|
||||
layer.alert("待签名内容不得为空")
|
||||
return
|
||||
}
|
||||
var sign = this.sign_form.sign;
|
||||
if(this.sign_form.data_type == 'base64'){
|
||||
sign = base64ToHex(sign)
|
||||
}
|
||||
var verifyResult = sm2.doVerifySignature(this.sign_form.data, sign, this.getPublicKey(), {der: this.sign_form.type, hash: this.sign_form.hash, userId: this.sign_form.userid})
|
||||
if(verifyResult){
|
||||
layer.alert('验签成功,签名正确', {icon:1})
|
||||
}else{
|
||||
layer.alert('验签失败,签名不正确', {icon:2})
|
||||
}
|
||||
},
|
||||
sign() {
|
||||
if (this.mutipart_from.public_key == '' || this.mutipart_from.private_key == ''){
|
||||
layer.alert("SM2公钥和私钥不能为空");return
|
||||
}
|
||||
if (this.sign_form.data === "") {
|
||||
layer.alert("待签名内容不得为空")
|
||||
return
|
||||
}
|
||||
this.sign_form.sign = sm2.doSignature(this.sign_form.data, this.getPrivateKey(), {der: this.sign_form.type, hash: this.sign_form.hash, userId: this.sign_form.userid});
|
||||
if(this.sign_form.data_type == 'base64'){
|
||||
this.sign_form.sign = hexToBase64(this.sign_form.sign)
|
||||
}
|
||||
},
|
||||
check() {
|
||||
if (this.mutipart_from.public_key == '' || this.mutipart_from.private_key == ''){
|
||||
layer.alert("SM2公钥和私钥不能为空");return
|
||||
}
|
||||
if(!sm2.verifyPublicKey(this.getPublicKey())){
|
||||
layer.alert("SM2公钥不正确");return
|
||||
}
|
||||
var sign = sm2.doSignature('cccyun', this.getPrivateKey(), {hash: true});
|
||||
var verifyResult = sm2.doVerifySignature('cccyun', sign, this.getPublicKey(), {hash: true})
|
||||
if(verifyResult){
|
||||
layer.alert('验证成功,公私钥匹配', {icon:1})
|
||||
}else{
|
||||
layer.alert('公私钥不匹配', {icon:2})
|
||||
}
|
||||
},
|
||||
output() {
|
||||
if(this.convert_from.type == 1 || this.convert_from.type == 2){
|
||||
if (this.convert_from.type == 1 && this.convert_from.cert == ''){
|
||||
layer.alert("SM2公钥证书不能为空");return
|
||||
}
|
||||
if (this.convert_from.type == 2 && this.convert_from.csr == ''){
|
||||
layer.alert("CSR不能为空");return
|
||||
}
|
||||
var that=this;
|
||||
return httpPost('/api/{$plugin.alias}/output', {
|
||||
...this.convert_from
|
||||
}, function(res){
|
||||
if (res.status === "ok") {
|
||||
that.mutipart_from.public_key = res.data
|
||||
if(that.mutipart_from.key_type == 'base64'){
|
||||
that.mutipart_from.public_key = hexToBase64(that.mutipart_from.public_key)
|
||||
}
|
||||
layer.alert(res.message, {icon:1})
|
||||
}else{
|
||||
layer.alert(res.message, {icon:2})
|
||||
}
|
||||
}, true)
|
||||
}else{
|
||||
if (this.mutipart_from.private_key == ''){
|
||||
layer.alert("SM2私钥不能为空");return
|
||||
}
|
||||
this.mutipart_from.public_key = sm2.getPublicKeyFromPrivateKey(this.getPrivateKey());
|
||||
if(this.mutipart_from.key_type == 'base64'){
|
||||
this.mutipart_from.public_key = hexToBase64(this.mutipart_from.public_key)
|
||||
}
|
||||
layer.alert('导出公钥成功', {icon:1})
|
||||
}
|
||||
},
|
||||
key_compress() {
|
||||
if (this.mutipart_from.public_key == ''){
|
||||
layer.alert("SM2公钥不能为空");return
|
||||
}
|
||||
if (this.mutipart_from.public_key.length == 66) return;
|
||||
try{
|
||||
this.mutipart_from.public_key = sm2.compressPublicKeyHex(this.getPublicKey())
|
||||
if(this.mutipart_from.key_type == 'base64'){
|
||||
this.mutipart_from.public_key = hexToBase64(this.mutipart_from.public_key)
|
||||
}
|
||||
layer.alert('公钥压缩成功', {icon:1})
|
||||
} catch (e) {
|
||||
layer.msg(e.message, {icon:2});
|
||||
}
|
||||
},
|
||||
key_uncompress() {
|
||||
if (this.mutipart_from.public_key == ''){
|
||||
layer.alert("SM2公钥不能为空");return
|
||||
}
|
||||
if (this.mutipart_from.public_key.length == 130) return;
|
||||
this.mutipart_from.public_key = decompressPublicKey(this.getPublicKey())
|
||||
if(this.mutipart_from.key_type == 'base64'){
|
||||
this.mutipart_from.public_key = hexToBase64(this.mutipart_from.public_key)
|
||||
}
|
||||
layer.alert('公钥还原成功', {icon:1})
|
||||
},
|
||||
download(name) {
|
||||
var content = this.mutipart_from[name]
|
||||
if(!content) return;
|
||||
var filename = name + '.txt'
|
||||
var blob = new Blob([content], {type: "application/force-download"});
|
||||
saveAs(blob, filename);
|
||||
}
|
||||
},
|
||||
})
|
||||
</script>
|
||||
{/block}
|
||||
+60
-5
@@ -19,6 +19,7 @@ class App extends Plugin
|
||||
"private_key_bits" => 1024,
|
||||
//选择在创建CSR时应该使用哪些扩展。可选值有 OPENSSL_KEYTYPE_DSA, OPENSSL_KEYTYPE_DH, OPENSSL_KEYTYPE_RSA 或 OPENSSL_KEYTYPE_EC. 默认值是 OPENSSL_KEYTYPE_RSA.
|
||||
"private_key_type" => OPENSSL_KEYTYPE_RSA,
|
||||
"curve_name" => ''
|
||||
];
|
||||
private $passPhrase;
|
||||
private $rsaPrivateKey;
|
||||
@@ -28,6 +29,7 @@ class App extends Plugin
|
||||
private $origin;
|
||||
private $coded;
|
||||
private $opensslPadding;
|
||||
private $opensslAlgo = 1;
|
||||
|
||||
|
||||
public function __construct()
|
||||
@@ -35,13 +37,16 @@ class App extends Plugin
|
||||
$this->passPhrase = request()->param("pass_phrase");
|
||||
if($this->passPhrase == '') $this->passPhrase = null;
|
||||
$this->rsaPrivateKey = request()->param("private_key");
|
||||
$this->rsaPublicKey = request()->param("public_key");
|
||||
$this->config["private_key_bits"] = request()->param("private_key_bits");
|
||||
$this->rsaPublicKey = $this->getPemKey(request()->param("public_key"), 'PUBLIC KEY');
|
||||
$this->config["private_key_type"] = request()->param("private_key_type/d");
|
||||
$this->config["private_key_bits"] = request()->param("private_key_bits/d");
|
||||
$this->config["curve_name"] = request()->param("curve_name");
|
||||
$this->data = request()->param("data");
|
||||
$this->origin = request()->param("origin");
|
||||
$this->coded = request()->param("coded");
|
||||
$this->sign = request()->param("sign");
|
||||
$this->opensslPadding = request()->param("openssl_padding");
|
||||
$this->opensslAlgo = intval(request()->param("openssl_algo"));
|
||||
}
|
||||
|
||||
public function index()
|
||||
@@ -179,7 +184,7 @@ class App extends Plugin
|
||||
public function sign()
|
||||
{
|
||||
try {
|
||||
if (openssl_sign($this->data, $ret, $this->getRsaPrivateKey())) {
|
||||
if (openssl_sign($this->data, $ret, $this->getRsaPrivateKey(), $this->opensslAlgo)) {
|
||||
$ret = base64_encode($ret);
|
||||
return msg('ok', 'success', $ret);
|
||||
}
|
||||
@@ -203,7 +208,7 @@ class App extends Plugin
|
||||
$ret = false;
|
||||
$sign = base64_decode($this->sign);
|
||||
if ($sign !== false) {
|
||||
if (openssl_verify($this->data, $sign, $this->rsaPublicKey) === 1) {
|
||||
if (openssl_verify($this->data, $sign, $this->rsaPublicKey, $this->opensslAlgo) === 1) {
|
||||
$ret = true;
|
||||
}
|
||||
}
|
||||
@@ -231,7 +236,25 @@ class App extends Plugin
|
||||
|
||||
public function output()
|
||||
{
|
||||
$type = input('post.type/d');
|
||||
try {
|
||||
if($type == 1){
|
||||
$cert = input('post.cert');
|
||||
$pkey_res = openssl_pkey_get_public($cert);
|
||||
if(!$pkey_res){
|
||||
return msg('error', '导出失败,公钥证书不正确');
|
||||
}
|
||||
$pkey_detail = openssl_pkey_get_details($pkey_res);
|
||||
return msg('ok', '导出成功', $pkey_detail['key']);
|
||||
}elseif($type == 2){
|
||||
$csr = input('post.csr');
|
||||
$pkey_res = openssl_csr_get_public_key($csr);
|
||||
if(!$pkey_res){
|
||||
return msg('error', '导出失败,CSR不正确');
|
||||
}
|
||||
$pkey_detail = openssl_pkey_get_details($pkey_res);
|
||||
return msg('ok', '导出成功', $pkey_detail['key']);
|
||||
}
|
||||
$openssl_pkey_get_details = openssl_pkey_get_details($this->getRsaPrivateKey());
|
||||
if ($openssl_pkey_get_details && !empty($openssl_pkey_get_details['key'])) {
|
||||
return msg('ok', '导出成功', $openssl_pkey_get_details['key']);
|
||||
@@ -242,15 +265,47 @@ class App extends Plugin
|
||||
}
|
||||
}
|
||||
|
||||
public function key_encrypt()
|
||||
{
|
||||
try {
|
||||
$privateKey = openssl_pkey_get_private($this->getPemKey($this->rsaPrivateKey, 'PRIVATE KEY'));
|
||||
if ($privateKey === false) {
|
||||
throw new \Exception('私钥不正确或已被加密');
|
||||
}
|
||||
openssl_pkey_export($privateKey, $encrypted, $this->passPhrase);
|
||||
return msg('ok', '加密成功', $encrypted);
|
||||
} catch (\Exception $e) {
|
||||
return msg('error', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function key_decrypt()
|
||||
{
|
||||
try {
|
||||
openssl_pkey_export($this->getRsaPrivateKey(), $decrypted);
|
||||
return msg('ok', '解密成功', $decrypted);
|
||||
} catch (\Exception $e) {
|
||||
return msg('error', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|mixed|null
|
||||
*/
|
||||
public function getRsaPrivateKey()
|
||||
{
|
||||
$privateKey = openssl_pkey_get_private($this->rsaPrivateKey, $this->passPhrase);
|
||||
$privateKey = openssl_pkey_get_private($this->getPemKey($this->rsaPrivateKey, $this->passPhrase ? 'ENCRYPTED PRIVATE KEY' : 'PRIVATE KEY'), $this->passPhrase);
|
||||
if ($privateKey === false) {
|
||||
throw new \Exception('私钥不正确或密码错误');
|
||||
}
|
||||
return $privateKey;
|
||||
}
|
||||
|
||||
private function getPemKey($str, $type){
|
||||
if(empty($str) || strpos($str, '-----BEGIN')!==false) return $str;
|
||||
$pem = "-----BEGIN ".$type."-----\n" .
|
||||
wordwrap($str, 64, "\n", true) .
|
||||
"\n-----END ".$type."-----";
|
||||
return $pem;
|
||||
}
|
||||
}
|
||||
+245
-73
@@ -6,21 +6,34 @@
|
||||
<div class="card card-preview">
|
||||
<div class="card-inner mt-3">
|
||||
<div class="nya-title nk-ibx-action-item progress-rating">
|
||||
<span class="nk-menu-text font-weight-bold">RSA公私钥生成、验证、加密和解密</span>
|
||||
<span class="nk-menu-text font-weight-bold">RSA公私钥生成、签名、加解密</span>
|
||||
</div>
|
||||
<ul class="nav nav-tabs">
|
||||
<li class="nav-item"><a class="nav-link" @click="show=0" :class="{'active':show===0}" href="javascript:">RSA公私钥生成</a></li>
|
||||
<li class="nav-item"><a class="nav-link" @click="show=1" :class="{'active':show===1}" href="javascript:">RSA加密解密</a></li>
|
||||
<li class="nav-item"><a class="nav-link" @click="show=2" :class="{'active':show===2}" href="javascript:">RSA签名验签</a></li>
|
||||
<li class="nav-item"><a class="nav-link" @click="show=3" :class="{'active':show===3}" href="javascript:">RSA公私钥对验证</a></li>
|
||||
<li class="nav-item"><a class="nav-link" @click="show=4" :class="{'active':show===4}" href="javascript:">RSA公钥导出</a></li>
|
||||
<li class="nav-item"><a class="nav-link" @click="show=0" :class="{'active':show===0}" href="javascript:">公私钥生成</a></li>
|
||||
<li class="nav-item"><a class="nav-link" @click="show=1" :class="{'active':show===1}" href="javascript:">加密解密</a></li>
|
||||
<li class="nav-item"><a class="nav-link" @click="show=2" :class="{'active':show===2}" href="javascript:">签名验签</a></li>
|
||||
<li class="nav-item"><a class="nav-link" @click="show=3" :class="{'active':show===3}" href="javascript:">公私钥对验证</a></li>
|
||||
<li class="nav-item"><a class="nav-link" @click="show=4" :class="{'active':show===4}" href="javascript:">公钥导出</a></li>
|
||||
<li class="nav-item"><a class="nav-link" @click="show=5" :class="{'active':show===5}" href="javascript:">私钥加解密</a></li>
|
||||
</ul>
|
||||
<div class="tab-content">
|
||||
<div class="tab-pane" id="tabItem1" :class="{'active':show===0}">
|
||||
<div class="form-group">
|
||||
<label class="form-label">密钥算法</label>
|
||||
<select class="form-control" v-model="generate_from.private_key_type">
|
||||
<option v-for="v,index in private_key_type" :value="index">{{v}}</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group" v-if="generate_from.private_key_type!=3">
|
||||
<label class="form-label">密钥长度</label>
|
||||
<select class="form-control" v-model="generate_from.private_key_bits">
|
||||
<option v-for="v in [512,1024,2048,4096]" :value="v">{{v}} bit</option>
|
||||
<option v-for="v in private_key_bits" :value="v">{{v}} bit</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group" v-if="generate_from.private_key_type==3">
|
||||
<label class="form-label">曲线名称</label>
|
||||
<select class="form-control" v-model="generate_from.curve_name">
|
||||
<option v-for="v in curve_name" :value="v">{{v}}</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
@@ -34,18 +47,25 @@
|
||||
</button>
|
||||
<div class="d-flex flex-wrap">
|
||||
<div class="form-group flex-fill mr-2">
|
||||
<label class="form-label">RSA公钥</label>
|
||||
<label class="form-label">RSA公钥 <em class="icon ni ni-download" title="下载保存" v-show="mutipart_from.public_key" @click="download('public_key')"></em></label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" rows="8" v-model="mutipart_from.public_key"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group flex-fill">
|
||||
<label class="form-label">RSA私钥</label>
|
||||
<label class="form-label">RSA私钥 <em class="icon ni ni-download" title="下载保存" v-show="mutipart_from.private_key" @click="download('private_key')"></em></label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" rows="8" v-model="mutipart_from.private_key"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">密钥数据类型</label>
|
||||
<select class="form-control" v-model="mutipart_from.key_type">
|
||||
<option value="pem">PEM标准格式</option>
|
||||
<option value="base64">Base64无换行</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<div class="tab-pane" id="tabItem1" :class="{'active':show===1}">
|
||||
<div class="form-group">
|
||||
@@ -61,12 +81,6 @@
|
||||
<option :value="1">使用私钥加密,使用公钥解密</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">私钥密码</label>
|
||||
<div class="form-control-wrap">
|
||||
<input type="text" v-model="mutipart_from.pass_phrase" placeholder="没有密码请留空" class="form-control">
|
||||
</div>
|
||||
</div>
|
||||
<div class="row pt-1 pb-1 mb-3">
|
||||
<div class="col-6">
|
||||
<button class="btn btn-dim btn-outline-secondary btn-block card-link" @click="encrypt">
|
||||
@@ -79,20 +93,6 @@
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="d-flex flex-wrap">
|
||||
<div class="form-group flex-fill mr-2">
|
||||
<label class="form-label">RSA公钥</label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" rows="8" v-model="mutipart_from.public_key"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group flex-fill">
|
||||
<label class="form-label">RSA私钥</label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" rows="8" v-model="mutipart_from.private_key"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">原文,未加密的内容</label>
|
||||
<div class="form-control-wrap">
|
||||
@@ -113,12 +113,6 @@
|
||||
<option v-for="v,index in openssl_algo" :value="index+1">{{v}}</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">私钥密码</label>
|
||||
<div class="form-control-wrap">
|
||||
<input type="text" v-model="mutipart_from.pass_phrase" placeholder="没有密码请留空" class="form-control">
|
||||
</div>
|
||||
</div>
|
||||
<div class="row pt-1 pb-1 mb-3">
|
||||
<div class="col-6">
|
||||
<button class="btn btn-dim btn-outline-secondary btn-block card-link" @click="sign">
|
||||
@@ -131,20 +125,6 @@
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="d-flex flex-wrap">
|
||||
<div class="form-group flex-fill mr-2">
|
||||
<label class="form-label">RSA公钥</label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" rows="8" v-model="mutipart_from.public_key"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group flex-fill">
|
||||
<label class="form-label">RSA私钥</label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" rows="8" v-model="mutipart_from.private_key"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">待签名,验签原始内容</label>
|
||||
<div class="form-control-wrap">
|
||||
@@ -159,52 +139,119 @@
|
||||
</div>
|
||||
</div>
|
||||
<div class="tab-pane" id="tabItem1" :class="{'active':show===3}">
|
||||
<div class="d-flex flex-wrap">
|
||||
<div class="form-group flex-fill mr-2">
|
||||
<label class="form-label">RSA公钥</label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" rows="8" v-model="mutipart_from.public_key"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group flex-fill">
|
||||
<label class="form-label">RSA私钥</label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" rows="8" v-model="mutipart_from.private_key"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">私钥密码</label>
|
||||
<div class="form-control-wrap">
|
||||
<input type="text" v-model="mutipart_from.pass_phrase" placeholder="没有密码请留空" class="form-control">
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">密钥数据类型</label>
|
||||
<select class="form-control" v-model="mutipart_from.key_type">
|
||||
<option value="pem">PEM标准格式</option>
|
||||
<option value="base64">Base64无换行</option>
|
||||
</select>
|
||||
</div>
|
||||
<button class="btn btn-dim btn-outline-secondary btn-block card-link mb-3" @click="check">
|
||||
验证
|
||||
</button>
|
||||
</div>
|
||||
<div class="tab-pane" id="tabItem1" :class="{'active':show===4}">
|
||||
<div class="form-group">
|
||||
<label class="form-label">导出方式</label>
|
||||
<select class="form-control" v-model="convert_from.type">
|
||||
<option :value="0">从私钥导出公钥</option>
|
||||
<option :value="1">从公钥证书导出公钥</option>
|
||||
<option :value="2">从CSR导出公钥</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="d-flex flex-wrap">
|
||||
<div class="form-group flex-fill mr-2">
|
||||
<label class="form-label">RSA公钥</label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" rows="8" v-model="mutipart_from.public_key"></textarea>
|
||||
<textarea class="form-control" rows="8" v-model="mutipart_from.public_key" placeholder="导出结果"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group flex-fill">
|
||||
<div class="form-group flex-fill" v-show="convert_from.type==0">
|
||||
<label class="form-label">RSA私钥</label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" rows="8" v-model="mutipart_from.private_key"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group flex-fill" v-show="convert_from.type==1">
|
||||
<label class="form-label">RSA公钥证书</label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" rows="8" v-model="convert_from.cert" placeholder="-----BEGIN CERTIFICATE-----"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group flex-fill" v-show="convert_from.type==2">
|
||||
<label class="form-label">CSR</label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" rows="8" v-model="convert_from.csr" placeholder="-----BEGIN CERTIFICATE REQUEST-----"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="tab-pane" id="tabItem1" :class="{'active':show===4}">
|
||||
<div class="form-group">
|
||||
<div class="form-group" v-show="convert_from.type==0">
|
||||
<label class="form-label">私钥密码</label>
|
||||
<div class="form-control-wrap">
|
||||
<input type="text" v-model="mutipart_from.pass_phrase" placeholder="没有密码请留空" class="form-control">
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">密钥数据类型</label>
|
||||
<select class="form-control" v-model="mutipart_from.key_type">
|
||||
<option value="pem">PEM标准格式</option>
|
||||
<option value="base64">Base64无换行</option>
|
||||
</select>
|
||||
</div>
|
||||
<button class="btn btn-dim btn-outline-secondary btn-block card-link mb-3" @click="output">
|
||||
导出
|
||||
</button>
|
||||
<div class="d-flex flex-wrap">
|
||||
<div class="form-group flex-fill mr-2">
|
||||
<label class="form-label">RSA公钥</label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" rows="8" v-model="mutipart_from.public_key"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<div class="tab-pane" id="tabItem1" :class="{'active':show===5}">
|
||||
<div class="form-group">
|
||||
<label class="form-label">RSA私钥</label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" rows="6" v-model="mutipart_from.private_key"></textarea>
|
||||
</div>
|
||||
<div class="form-group flex-fill">
|
||||
<label class="form-label">RSA私钥</label>
|
||||
<div class="form-control-wrap">
|
||||
<textarea class="form-control" rows="8" v-model="mutipart_from.private_key"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">私钥密码</label>
|
||||
<div class="form-control-wrap">
|
||||
<input type="text" v-model="mutipart_from.pass_phrase" placeholder="" class="form-control">
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">密钥数据类型</label>
|
||||
<select class="form-control" v-model="mutipart_from.key_type">
|
||||
<option value="pem">PEM标准格式</option>
|
||||
<option value="base64">Base64无换行</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="row pt-1 pb-1 mb-3">
|
||||
<div class="col-6">
|
||||
<button class="btn btn-dim btn-outline-secondary btn-block card-link" @click="key_encrypt">
|
||||
加密
|
||||
</button>
|
||||
</div>
|
||||
<div class="col-6">
|
||||
<button class="btn btn-dim btn-outline-secondary btn-block card-link" @click="key_decrypt">
|
||||
解密
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -216,11 +263,43 @@
|
||||
{/block}
|
||||
{block name="script"}
|
||||
<script src="{$cdn_cdnjs}vue/2.6.14/vue.min.js"></script>
|
||||
<script src="{$cdn_cdnjs}FileSaver.js/2.0.5/FileSaver.min.js"></script>
|
||||
<script>
|
||||
function pemToBase64(pem) {
|
||||
var lines = pem.split('\n');
|
||||
var encoded = '';
|
||||
for (var i = 0; i < lines.length; i++) {
|
||||
if (lines[i].trim().length > 0 && lines[i].indexOf('-----BEGIN') === -1 && lines[i].indexOf('-----END') === -1) {
|
||||
encoded += lines[i].trim();
|
||||
}
|
||||
}
|
||||
return encoded;
|
||||
}
|
||||
function base64ToPem(base64Key, type) {
|
||||
var pemHeader = '-----BEGIN ' + type + '-----\n';
|
||||
var pemFooter = '\n-----END ' + type + '-----\n';
|
||||
var chunkSize = 64;
|
||||
var base64KeyWithBreaks = base64Key.match(new RegExp('.{1,' + chunkSize + '}', 'g')).join('\n');
|
||||
var pemKey = pemHeader + base64KeyWithBreaks + pemFooter;
|
||||
return pemKey;
|
||||
}
|
||||
new Vue({
|
||||
el: '#app',
|
||||
data: {
|
||||
show: parseInt(localStorage.getItem('{$plugin.alias}_show') || 0),
|
||||
show: 0,
|
||||
private_key_type: [
|
||||
'RSA',
|
||||
'DSA',
|
||||
'DH',
|
||||
'ECC'
|
||||
],
|
||||
private_key_bits: [512,1024,2048,4096],
|
||||
curve_name: [
|
||||
'prime256v1',
|
||||
'secp256k1',
|
||||
'secp384r1',
|
||||
'secp521r1',
|
||||
],
|
||||
openssl_padding: [
|
||||
'OPENSSL_PKCS1_PADDING',
|
||||
'OPENSSL_SSLV23_PADDING',
|
||||
@@ -239,13 +318,21 @@
|
||||
'OPENSSL_ALGO_SHA512',
|
||||
'OPENSSL_ALGO_RMD160',
|
||||
],
|
||||
mutipart_from: JSON.parse(localStorage.getItem('{$plugin.alias}_mutipart_from')) || {
|
||||
mutipart_from: JSON.parse(localStorage.getItem('{$plugin.alias}_from')) || {
|
||||
public_key: '',
|
||||
private_key: '',
|
||||
pass_phrase: '',
|
||||
key_type: 'pem',
|
||||
},
|
||||
convert_from: {
|
||||
type: 0,
|
||||
cert: '',
|
||||
csr: ''
|
||||
},
|
||||
generate_from: {
|
||||
private_key_bits: 1024,
|
||||
private_key_type: 0,
|
||||
private_key_bits: 2048,
|
||||
curve_name: 'prime256v1'
|
||||
},
|
||||
crypto_from: {
|
||||
openssl_padding: 1,
|
||||
@@ -260,14 +347,34 @@
|
||||
},
|
||||
},
|
||||
watch: {
|
||||
show(newVal) {
|
||||
localStorage.setItem('{$plugin.alias}_show', newVal)
|
||||
},
|
||||
mutipart_from: {
|
||||
handler: (newVal) => {
|
||||
localStorage.setItem('{$plugin.alias}_mutipart_from', JSON.stringify(newVal))
|
||||
localStorage.setItem('{$plugin.alias}_from', JSON.stringify(newVal))
|
||||
},
|
||||
deep: true,
|
||||
},
|
||||
"mutipart_from.key_type"(newVal) {
|
||||
if(this.mutipart_from.public_key != ''){
|
||||
if(newVal == 'pem'){
|
||||
this.mutipart_from.public_key = base64ToPem(this.mutipart_from.public_key, 'PUBLIC KEY')
|
||||
}else{
|
||||
this.mutipart_from.public_key = pemToBase64(this.mutipart_from.public_key)
|
||||
}
|
||||
}
|
||||
if(this.mutipart_from.private_key != ''){
|
||||
if(newVal == 'pem'){
|
||||
this.mutipart_from.private_key = base64ToPem(this.mutipart_from.private_key, this.mutipart_from.pass_phrase ? 'ENCRYPTED PRIVATE KEY' : 'PRIVATE KEY')
|
||||
}else{
|
||||
this.mutipart_from.private_key = pemToBase64(this.mutipart_from.private_key)
|
||||
}
|
||||
}
|
||||
},
|
||||
"generate_from.private_key_type"(newVal) {
|
||||
if(newVal == 2){
|
||||
this.private_key_bits = [512,1024,2048]
|
||||
}else{
|
||||
this.private_key_bits = [512,1024,2048,4096]
|
||||
}
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
@@ -279,6 +386,10 @@
|
||||
}, function(data){
|
||||
that.mutipart_from.public_key = data.public_key
|
||||
that.mutipart_from.private_key = data.private_key
|
||||
if(that.mutipart_from.key_type == 'base64'){
|
||||
that.mutipart_from.public_key = pemToBase64(that.mutipart_from.public_key)
|
||||
that.mutipart_from.private_key = pemToBase64(that.mutipart_from.private_key)
|
||||
}
|
||||
})
|
||||
},
|
||||
encrypt() {
|
||||
@@ -367,21 +478,82 @@
|
||||
}, true)
|
||||
},
|
||||
output() {
|
||||
if (this.mutipart_from.private_key == ''){
|
||||
if (this.convert_from.type == 0 && this.mutipart_from.private_key == ''){
|
||||
layer.alert("RSA私钥不能为空");return
|
||||
}
|
||||
if (this.convert_from.type == 1 && this.convert_from.cert == ''){
|
||||
layer.alert("RSA公钥证书不能为空");return
|
||||
}
|
||||
if (this.convert_from.type == 2 && this.convert_from.csr == ''){
|
||||
layer.alert("CSR不能为空");return
|
||||
}
|
||||
var that=this;
|
||||
return httpPost('/api/{$plugin.alias}/output', {
|
||||
...this.convert_from,
|
||||
...this.mutipart_from
|
||||
}, function(res){
|
||||
if (res.status === "ok") {
|
||||
that.mutipart_from.public_key = res.data
|
||||
if(that.mutipart_from.key_type == 'base64'){
|
||||
that.mutipart_from.public_key = pemToBase64(that.mutipart_from.public_key)
|
||||
}
|
||||
layer.alert(res.message, {icon:1})
|
||||
}else{
|
||||
layer.alert(res.message, {icon:2})
|
||||
}
|
||||
}, true)
|
||||
},
|
||||
key_encrypt() {
|
||||
if (this.mutipart_from.private_key == ''){
|
||||
layer.alert("RSA私钥不能为空");return
|
||||
}
|
||||
if (this.mutipart_from.pass_phrase == ''){
|
||||
layer.alert("私钥密码不能为空");return
|
||||
}
|
||||
var that=this;
|
||||
return httpPost('/api/{$plugin.alias}/key_encrypt', {
|
||||
...this.mutipart_from
|
||||
}, function(res){
|
||||
if (res.status === "ok") {
|
||||
that.mutipart_from.private_key = res.data
|
||||
if(that.mutipart_from.key_type == 'base64'){
|
||||
that.mutipart_from.private_key = pemToBase64(that.mutipart_from.private_key)
|
||||
}
|
||||
layer.alert(res.message, {icon:1})
|
||||
}else{
|
||||
layer.alert(res.message, {icon:2})
|
||||
}
|
||||
}, true)
|
||||
},
|
||||
key_decrypt() {
|
||||
if (this.mutipart_from.private_key == ''){
|
||||
layer.alert("RSA私钥不能为空");return
|
||||
}
|
||||
if (this.mutipart_from.pass_phrase == ''){
|
||||
layer.alert("私钥密码不能为空");return
|
||||
}
|
||||
var that=this;
|
||||
return httpPost('/api/{$plugin.alias}/key_decrypt', {
|
||||
...this.mutipart_from
|
||||
}, function(res){
|
||||
if (res.status === "ok") {
|
||||
that.mutipart_from.private_key = res.data
|
||||
if(that.mutipart_from.key_type == 'base64'){
|
||||
that.mutipart_from.private_key = pemToBase64(that.mutipart_from.private_key)
|
||||
}
|
||||
layer.alert(res.message, {icon:1})
|
||||
}else{
|
||||
layer.alert(res.message, {icon:2})
|
||||
}
|
||||
}, true)
|
||||
},
|
||||
download(name) {
|
||||
var content = this.mutipart_from[name]
|
||||
if(!content) return;
|
||||
var filename = name + (this.mutipart_from.key_type == 'base64' ? '.txt' : '.pem')
|
||||
var blob = new Blob([content], {type: "application/force-download"});
|
||||
saveAs(blob, filename);
|
||||
}
|
||||
},
|
||||
})
|
||||
</script>
|
||||
|
||||
@@ -141,9 +141,6 @@ new Vue({
|
||||
const index = this.tasks.push(speed) - 1
|
||||
return axios.request({
|
||||
url: this.set.input,
|
||||
params: {
|
||||
[id]: id,
|
||||
},
|
||||
cancelToken: this.cancelSource.token,
|
||||
onDownloadProgress: function (progressEvent) {
|
||||
// 处理原生进度事件
|
||||
|
||||
@@ -50,6 +50,7 @@
|
||||
{block name="script"}
|
||||
<script src="{$cdn_cdnjs}vue/2.6.14/vue.min.js"></script>
|
||||
<script src="{$cdn_cdnjs}crypto-js/4.1.1/crypto-js.min.js"></script>
|
||||
<script src="/assets/sm/sm3.js"></script>
|
||||
<script>
|
||||
new Vue({
|
||||
el: '#app',
|
||||
@@ -73,20 +74,30 @@
|
||||
'sha512',
|
||||
'sha384',
|
||||
'ripemd160',
|
||||
'sm3',
|
||||
];
|
||||
|
||||
let arr = {}
|
||||
if(this.set.key == ''){
|
||||
for (const v of list) {
|
||||
arr[v] = CryptoJS[v.toUpperCase()](this.set.text).toString()
|
||||
if(v == 'sm3') arr[v] = sm3(this.set.text)
|
||||
else arr[v] = CryptoJS[v.toUpperCase()](this.set.text).toString()
|
||||
}
|
||||
}else{
|
||||
for (const v of list) {
|
||||
arr['hmac-' + v] = CryptoJS['Hmac' + v.toUpperCase()](this.set.text, this.set.key).toString()
|
||||
if(v == 'sm3') arr['hmac-' + v] = sm3(this.set.text, {key: this.stringToHex(this.set.key)})
|
||||
else arr['hmac-' + v] = CryptoJS['Hmac' + v.toUpperCase()](this.set.text, this.set.key).toString()
|
||||
}
|
||||
}
|
||||
this.result = arr
|
||||
},
|
||||
stringToHex(str){
|
||||
var val="";
|
||||
for(var i = 0; i < str.length; i++){
|
||||
val += str.charCodeAt(i).toString(16);
|
||||
}
|
||||
return val;
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
+11
-3
@@ -119,16 +119,24 @@ class App extends Plugin
|
||||
$headers = ['Origin: https://beian.miit.gov.cn'];
|
||||
$url = 'https://hlwicpfwc.miit.gov.cn/icpproject_query/api/auth';
|
||||
$post = 'authKey='.$authKey.'&timeStamp='.$timeStamp;
|
||||
$response = get_curl($url, $post, $referer, 0, 0, 0, 0, $headers);
|
||||
$arr = json_decode($response, true);
|
||||
$response = get_curl($url, $post, $referer, 0, 1, 0, 0, $headers);
|
||||
$body = substr($response, strpos($response, '{"'));
|
||||
$arr = json_decode($body, true);
|
||||
if(isset($arr['code']) && $arr['code']==200){
|
||||
$cookie = '';
|
||||
preg_match_all('/set-cookie: (.*?);/i', $response, $matchs);
|
||||
foreach ($matchs[1] as $val) {
|
||||
if(substr($val,-1)=='=')continue;
|
||||
$cookie.=$val.'; ';
|
||||
}
|
||||
|
||||
$token = $arr['params']['bussiness'];
|
||||
|
||||
$url = 'https://hlwicpfwc.miit.gov.cn/icpproject_query/api/icpAbbreviateInfo/queryByCondition';
|
||||
$post = json_encode(['pageNum'=>'','pageSize'=>'','unitName'=>$domain,'serviceType'=>1]);
|
||||
$headers[] = 'Content-Type: application/json; charset=UTF-8';
|
||||
$headers[] = 'token: '.$token;
|
||||
$response = get_curl($url, $post, $referer, 0, 0, 0, 0, $headers);
|
||||
$response = get_curl($url, $post, $referer, $cookie, 0, 0, 0, $headers);
|
||||
$arr = json_decode($response, true);
|
||||
if(isset($arr['code']) && $arr['code']==200){
|
||||
$list = [];
|
||||
|
||||
Reference in New Issue
Block a user