You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
2.1 KiB
PHP
67 lines
2.1 KiB
PHP
<?php
|
|
namespace App\Lib;
|
|
class HSM
|
|
{
|
|
protected static $baseurl="http://223.70.139.221:2018";
|
|
protected static $keyIndex=1;
|
|
protected static $encAlg="SM4/CBC/PKCS5Padding";
|
|
protected static $iv="31323334353637383132333435363738";
|
|
|
|
|
|
//加密
|
|
public static function HsmEncrypt($str){
|
|
$str = bin2hex($str);
|
|
$url= self::$baseurl."/api/hsm/sym/symEncryptInternalForKEK";
|
|
$data=[
|
|
"keyIndex"=>self::$keyIndex,
|
|
"encAlg"=>self::$encAlg,
|
|
"iv"=>self::$iv,
|
|
"plainData"=>$str
|
|
];
|
|
$data=json_encode($data);
|
|
$encryptStr=self::post($url,$data);
|
|
$r_data=json_decode($encryptStr, true);
|
|
// dd($r_data);
|
|
if($r_data['status']==0){
|
|
return ['encrypt_str'=>$r_data['body']['cipherData'],'status'=>true];
|
|
}else{
|
|
return ['status'=>false];
|
|
}
|
|
}
|
|
//解密
|
|
public static function HsmDecrypt($str){
|
|
$url= self::$baseurl."/api/hsm/sym/symDecryptInternalForKEK";
|
|
$data=[
|
|
"keyIndex"=>self::$keyIndex,
|
|
"encAlg"=>self::$encAlg,
|
|
"iv"=>self::$iv,
|
|
"cipherData"=>$str
|
|
];
|
|
$data=json_encode($data);
|
|
$encryptStr=self::post($url,$data);
|
|
$r_data=json_decode($encryptStr, true);
|
|
if($r_data['status']==0){
|
|
return ['decrypt_str'=>hex2bin($r_data['body']['plain']),'status'=>true];
|
|
}else{
|
|
return ['status'=>false];
|
|
}
|
|
}
|
|
public function post($url, $data_string)
|
|
{
|
|
$curl = curl_init();
|
|
curl_setopt($curl, CURLOPT_URL, $url);
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($curl, CURLOPT_POST, true);
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER, [
|
|
'Content-Type: application/json; charset=utf-8',
|
|
'Content-Length: ' . strlen($data_string)
|
|
]);
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
|
|
$r = curl_exec($curl);
|
|
curl_close($curl);
|
|
return $r;
|
|
}
|
|
}
|