php加密方式:AES-128-ECB

//RSA签名排列,按键值字母排序
	public function encryptRsaStr($param)
	{
		//参数排序
		ksort($param);
		unset($param['notify_url']);
		unset($param['async_notify_url']);
		return urldecode(http_build_query($param));
	}

	//AES加密排列,按键值字母排序
	function encryptAesStr($param)
	{
		//参数排序
		ksort($param);
		return json_encode($param,true);
	}

	//AES-128-ECB加密
	function aes_encrypt($data, $key) {
		$data =  openssl_encrypt($data, 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
		return base64_encode($data);
	}

	//AES-128-ECB解密
	function aes_decrypt($data, $key) {
		$encrypted = base64_decode($data);
		return openssl_decrypt($encrypted, 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
	}
	
	//公钥加密
    public function PublicEncrypt($data){
        //openssl_public_encrypt($data,$encrypted,$this->pu_key);/ /公钥加密
        $crypto = '';
        foreach (str_split($data, 117) as $chunk) {
            openssl_public_encrypt($chunk, $encryptData, $this->pu_key);
            $crypto .= $encryptData;
        }
        $encrypted = $this->urlsafe_b64encode($crypto);
        return $encrypted;
    }

    //公钥解密  私钥加密的内容通过公钥解密
    public function PublicDecrypt($encrypted){
        //$encrypted = $this->urlsafe_b64decode($encrypted);
        $crypto = '';
        foreach (str_split($this->urlsafe_b64decode($encrypted), 128) as $chunk) {
            openssl_public_decrypt($chunk, $decryptData, $this->pu_key);
            $crypto .= $decryptData;
        }
        //openssl_public_decrypt($encrypted,$decrypted,$this->pu_key); //私钥加密的内容通过公钥可用解密出来
        return $crypto;
    }

    //私钥加密
    public function PrivateEncrypt($data){
        //openssl_private_encrypt($data,$encrypted,$this->pi_key);
        $crypto = '';
        foreach (str_split($data, 117) as $chunk) {
            openssl_private_encrypt($chunk, $encryptData, $this->pi_key);
            $crypto .= $encryptData;
        }
        $encrypted = $this->urlsafe_b64encode($crypto); //加密后的内容通常含有特殊字符,需要编码转换下,在网络间通过url传输时要注意base64编码是否是url安全的
        return $encrypted;
    }

    //私钥解密
    public function PrivateDecrypt($encrypted){
        $crypto = '';
        foreach (str_split($this->urlsafe_b64decode($encrypted), 128) as $chunk) {
            openssl_private_decrypt($chunk, $decryptData, $this->pi_key);
            $crypto .= $decryptData;
        }
        //$encrypted = $this->urlsafe_b64decode($encrypted);
        //openssl_private_decrypt($encrypted,$decrypted,$this->pi_key);
        return $crypto;
    }

    //加密码时把特殊符号替换成URL可以带的内容
    function urlsafe_b64encode($string) {
        $data = base64_encode($string);
        //$data = str_replace(array('+','/','='),array('-','_',''),$data);
        return $data;
    }

    //解密码时把转换后的符号替换特殊符号
    function urlsafe_b64decode($string) {
        $data = str_replace(array('-','_'),array('+','/'),$string);
        $mod4 = strlen($data) % 4;
        if ($mod4) {
            $data .= substr('====', $mod4);
        }
        return base64_decode($data);
    }

你可能感兴趣的:(后端,PHP,加密解密)