<?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'=>'私鑰不可用'];
}
//驗(yàn)證公鑰是否正確
$public_key = openssl_pkey_get_public($keyContent);
if(!$public_key){
return ['status'=>false,'messate'=>'公鑰不可用'];
}
//第一個(gè)參數(shù)是待加密的數(shù)據(jù)只能是string,第二個(gè)參數(shù)是加密后的數(shù)據(jù),第三個(gè)參數(shù)是openssl_pkey_get_public返回的資源類型,第四個(gè)參數(shù)是填充方式
$return_en = openssl_public_encrypt($string, $crypted, $public_key);
if(!$return_en){
return ['status'=>false,'messate'=>'公鑰錯(cuò)誤'];
}
$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'=>'私鑰不可用'];
}
//驗(yàn)證私鑰
$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'=>'私鑰不可用'];
}
//驗(yàn)證私鑰是否正確
$private_key = openssl_pkey_get_private($keyContent);
if(!$private_key){
return ['status'=>false,'messate'=>'私鑰不可用'];
}
//第一個(gè)參數(shù)是待加密的數(shù)據(jù)只能是string,第二個(gè)參數(shù)是加密后的數(shù)據(jù),第三個(gè)參數(shù)是openssl_pkey_get_public返回的資源類型,第四個(gè)參數(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'=>'私鑰不可用'];
}
//驗(yàn)證公鑰是否正確
$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 加解密
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。