AES RSA 加解密

2022-08-12  本文已影响0人  可望不可j
<?php
namespace App\Models;
use Illuminate\Support\Facades\Log;

class IcbcPayModel
{

    //AES秘钥
    protected $aesKey = "";
    //向量
    protected $hex_iv = "00000000000000000000000000000000";
    //公钥
    protected $publicKey = '';
    //私钥
    protected $privateKey = '';

    //地址
    protected $publicCerPath;

    //系统
    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'=>'公钥不可用'];
        }
        //第一个参数是待加密的数据只能是string,第二个参数是加密后的数据,第三个参数是openssl_pkey_get_public返回的资源类型,第四个参数是填充方式
        $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'=>'私钥不可用'];
        }
        //第一个参数是待加密的数据只能是string,第二个参数是加密后的数据,第三个参数是openssl_pkey_get_public返回的资源类型,第四个参数是填充方式
        $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;
    }
}


上一篇下一篇

猜你喜欢

热点阅读