php 代金券生成器,php微信支付v3版签名生成,代金券、微信支付分、支付即服务等...

/**

* V3加密*/

public function getEncrypt($str){//$str是待加密字符串

$public_key_path = '证书地址'; //看情况使用证书, 个别接口证书 使用的是 平台证书而不是 api证书$public_key = file_get_contents($public_key_path);$encrypted = '';if (openssl_public_encrypt($str,$encrypted,$public_key,OPENSSL_PKCS1_OAEP_PADDING)) {//base64编码

$sign = base64_encode($encrypted);

}else{throw new Exception('encrypt failed');

}return $sign;

}/**

* Decrypt AEAD_AES_256_GCM ciphertext V3签名解密

* @param stingr $aesKey V3签名

* @param string $associatedData AES GCM additional authentication data

* @param string $nonceStr AES GCM nonce

* @param string $ciphertext AES GCM cipher text

*

* @return string|bool Decrypted string on success or FALSE on failure*/

public function decryptToString($aesKey ,$associatedData, $nonceStr, $ciphertext)

{if (strlen($aesKey) != 32) {throw new InvalidArgumentException('无效的ApiV3Key,长度应为32个字节');

}$ciphertext = base64_decode($ciphertext , true);if (strlen($ciphertext) <= 16) {return false;

}//ext-sodium (default installed on >= PHP 7.2)

if(function_exists('sodium_crypto_aead_aes256gcm_is_available') &&sodium_crypto_aead_aes256gcm_is_available() ){return sodium_crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $aesKey);

}//ext-libsodium (need install libsodium-php 1.x via pecl)

if(function_exists('Sodiumcrypto_aead_aes256gcm_is_available') &&Sodiumcrypto_aead_aes256gcm_is_available()){return Sodiumcrypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $aesKey);

}//PHP >= 7.1

if(PHP_VERSION_ID >= 70100 && in_array('aes-256-gcm',openssl_get_cipher_methods()) ){$ctext = substr($ciphertext, 0, -16);$authTag = substr($ciphertext, -16);return openssl_decrypt($ctext, 'aes-256-gcm', $aesKey, OPENSSL_RAW_DATA, $nonceStr,$authTag, $associatedData);

}throw new RuntimeException('AEAD_AES_256_GCM需要PHP 7.1以上或者安装libsodium-php');

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值