<?php
namespace App\Models;
use Illuminate\Support\Facades\Log;
class IcbcPayModel
{
//AES秘鑰
protected $aesKey = "";
//向量
protected $hex_iv = "00000000000000000000000000000000";
//公鑰
protected $publicKey = '';
//私鑰
protected $privateKey = '';
//地址
protected $publicCerPath;
//系統(tǒng)
protected $school_rsa = [
'118'=>[
'public'=>'public.pem',
'private'=>'private.pem',
]
];
public function __construct()
{
$this->key = hash('sha256', $this->aesKey, true);
}
/**
* 公鑰-加密
* @param string $RSA_PUBLIC 公鑰
* @param string $string 需要加密的字符串
* @param bool $is_sssembly true|需要拼接 false|不需要
* @return array
*/
public function public_key_encryp($string,$school_id){
if(isset($this->school_rsa[$school_id]['public'])){
$this->publicKey = public_path() . "/cert/IcbcPayCert/".$school_id."/".$this->school_rsa[$school_id]['public'];
}else{
return ['status'=>false,'messate'=>'私鑰不可用'];
}
$keyContent = file_get_contents($this->publicKey);
if(!$keyContent){
return ['status'=>false,'messate'=>'私鑰不可用'];
}
//驗證公鑰是否正確
$public_key = openssl_pkey_get_public($keyContent);
if(!$public_key){
return ['status'=>false,'messate'=>'公鑰不可用'];
}
//第一個參數(shù)是待加密的數(shù)據(jù)只能是string倔喂,第二個參數(shù)是加密后的數(shù)據(jù),第三個參數(shù)是openssl_pkey_get_public返回的資源類型,第四個參數(shù)是填充方式
$return_en = openssl_public_encrypt($string, $crypted, $public_key);
if(!$return_en){
return ['status'=>false,'messate'=>'公鑰錯誤'];
}
$eb64_cry = base64_encode($crypted);
return ['status'=>true,'messate'=>'ok','data'=>$eb64_cry];
}
/**
* 私鑰-解密
* @param string $string 需要加密的字符串
* @return array
*/
public function private_key_decrypt($string,$school_id){
if($this->school_rsa[$school_id]['private']){
$this->privateKey = public_path() . "/cert/IcbcPayCert/".$school_id."/".$this->school_rsa[$school_id]['private'];
}else{
return ['status'=>false,'messate'=>'私鑰不可用'];
}
$keyContent = file_get_contents($this->privateKey);
if(!$keyContent){
return ['status'=>false,'messate'=>'私鑰不可用'];
}
//驗證私鑰
$private_key = openssl_pkey_get_private($keyContent);
if(!$private_key){
return ['status'=>false,'messate'=>'私鑰不可用'];
}
$return_de = openssl_private_decrypt(base64_decode($string), $decrypted, $private_key);
if(!$return_de){
return ['status'=>false,'messate'=>'解密失敗,請檢查私秘鑰'];
}
return ['status'=>true,'messate'=>'ok','data'=>$decrypted];
}
/**
* 私鑰-加密
* @param string $string 需要加密的字符串
* @return array
*/
public function private_key_encryp($string,$school_id){
if($this->school_rsa[$school_id]['private']){
$this->privateKey = public_path() . "/cert/IcbcPayCert/".$school_id."/".$this->school_rsa[$school_id]['private'];
}else{
return ['status'=>false,'messate'=>'私鑰不可用'];
}
$keyContent = file_get_contents($this->privateKey);
if(!$keyContent){
return ['status'=>false,'messate'=>'私鑰不可用'];
}
//驗證私鑰是否正確
$private_key = openssl_pkey_get_private($keyContent);
if(!$private_key){
return ['status'=>false,'messate'=>'私鑰不可用'];
}
//第一個參數(shù)是待加密的數(shù)據(jù)只能是string战转,第二個參數(shù)是加密后的數(shù)據(jù),第三個參數(shù)是openssl_pkey_get_public返回的資源類型,第四個參數(shù)是填充方式
$return_en = openssl_private_encrypt($string, $crypted, $private_key);
if(!$return_en){
return ['status'=>false,'messate'=>'加密失敗'];
}
$eb64_cry = base64_encode($crypted);
return ['status'=>true,'messate'=>'ok','data'=>$eb64_cry];
}
/**
* 公鑰-解密
* @param string $string 需要加密的字符串
* @return array
*/
public function public_key_decrypt($string,$school_id){
if(isset($this->school_rsa[$school_id]['public'])){
$this->publicKey = public_path() . "/cert/IcbcPayCert/".$school_id."/".$this->school_rsa[$school_id]['public'];
}else{
return ['status'=>false,'messate'=>'私鑰不可用'];
}
$keyContent = file_get_contents($this->publicKey);
if(!$keyContent){
return ['status'=>false,'messate'=>'私鑰不可用'];
}
//驗證公鑰是否正確
$public_key = openssl_pkey_get_public($keyContent);
if(!$public_key){
return ['status'=>false,'messate'=>'公鑰不可用'];
}
$return_en = openssl_public_decrypt(base64_decode($string), $decrypted, $public_key);
if(!$return_en){
return ['status'=>false,'messate'=>'解密失敗'];
}
return ['status'=>true,'messate'=>'ok','data'=>$decrypted];
}
/**
* AES 解密
* @param $input
* @return string
*/
public function encrypt($input)
{
$data = openssl_encrypt($input, 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA, $this->hexToStr($this->hex_iv));
$data = base64_encode($data);
return $data;
}
/**
* 解密
* @param $input
* @return false|string
*/
public function decrypt($input)
{
$decrypted = openssl_decrypt(base64_decode($input), 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA, $this->hexToStr($this->hex_iv));
return $decrypted;
}
public function hexToStr($hex){
$string='';
for ($i=0; $i < strlen($hex)-1; $i+=2){
$string .= chr(hexdec($hex[$i].$hex[$i+1]));
}
return $string;
}
}
AES RSA 加解密
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門糯钙,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人退腥,你說我怎么就攤上這事任岸。” “怎么了狡刘?”我有些...
- 正文 為了忘掉前任澜术,我火速辦了婚禮艺蝴,結果婚禮上,老公的妹妹穿的比我還像新娘鸟废。我一直安慰自己猜敢,他們只是感情好,可當我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著缩擂,像睡著了一般鼠冕。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上胯盯,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼君账!你這毒婦竟也來了繁堡?” 一聲冷哼從身側響起,我...
- 正文 年R本政府宣布中姜,位于F島的核電站,受9級特大地震影響跟伏,放射性物質發(fā)生泄漏丢胚。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一受扳、第九天 我趴在偏房一處隱蔽的房頂上張望携龟。 院中可真熱鬧,春花似錦勘高、人聲如沸骨宠。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽层亿。三九已至桦卒,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間匿又,已是汗流浹背方灾。 一陣腳步聲響...