| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- namespace app\extra\jhfPay;
- use InvalidArgumentException;
- use RuntimeException;
- class Utils
- {
- /**
- * AES加密函数(ECB模式,32位密钥)
- *
- * @param string $plaintext 待加密的明文
- * @param string $key 32字节(256位)密钥
- * @return string 加密后的Base64编码字符串
- */
- public static function aes_encrypt($plaintext, $key) {
- // 验证密钥长度(32字节 = 256位)
- if (strlen($key) !== 32) {
- throw new InvalidArgumentException('密钥长度必须为32字节');
- }
- // 执行加密(ECB模式,PKCS#7填充)
- $ciphertext = openssl_encrypt(
- $plaintext,
- 'AES-256-ECB',
- $key,
- OPENSSL_RAW_DATA
- );
- // 返回Base64编码结果
- return base64_encode($ciphertext);
- }
- /**
- * AES解密函数(ECB模式,32位密钥)
- *
- * @param string $ciphertext 待解密的Base64编码密文
- * @param string $key 32字节(256位)密钥
- * @return string 解密后的明文
- */
- public static function aes_decrypt($ciphertext, $key) {
- // 验证密钥长度(32字节 = 256位)
- if (strlen($key) !== 32) {
- throw new InvalidArgumentException('密钥长度必须为32字节');
- }
- // 解码Base64密文
- $ciphertext = base64_decode($ciphertext);
- // 执行解密(ECB模式,PKCS#7填充)
- return openssl_decrypt(
- $ciphertext,
- 'AES-256-ECB',
- $key,
- OPENSSL_RAW_DATA
- );
- }
- /**
- * RSA签名函数(SHA256withRSA,1024位密钥)
- *
- * @param string $data 待签名的数据
- * @param string $privateKeyText 私钥纯文本内容(不带PEM头和尾)
- * @return string 签名后的Base64编码字符串
- */
- public static function rsa_sign(string $data, string $privateKeyText): string
- {
- // 将纯文本私钥转换为PEM格式
- $privateKeyPem = "-----BEGIN PRIVATE KEY-----\n" .
- chunk_split($privateKeyText, 64, "\n") .
- "-----END PRIVATE KEY-----";
- // 创建私钥资源
- $privateKeyResource = openssl_pkey_get_private($privateKeyPem);
- if (!$privateKeyResource) {
- throw new InvalidArgumentException('私钥格式错误: ' . openssl_error_string());
- }
- // 生成签名(SHA256withRSA)
- $success = openssl_sign($data, $signature, $privateKeyResource, OPENSSL_ALGO_SHA256);
- // 释放资源
- unset($privateKey);
- // openssl_free_key($privateKeyResource);
- if (!$success) {
- throw new RuntimeException('签名失败: ' . openssl_error_string());
- }
- // 返回Base64编码的签名
- return base64_encode($signature);
- }
- /**
- * RSA验签函数(SHA256withRSA,1024位密钥)
- *
- * @param string $data 原始数据
- * @param string $signature 签名的Base64编码字符串
- * @param string $publicKeyText 公钥纯文本内容(不带PEM头和尾)
- * @return bool 验签结果
- */
- public static function rsa_verify(string $data, string $signature, string $publicKeyText): bool
- {
- // 将纯文本公钥转换为PEM格式
- $publicKeyPem = "-----BEGIN PUBLIC KEY-----\n" .
- chunk_split($publicKeyText, 64, "\n") .
- "-----END PUBLIC KEY-----";
- // 创建公钥资源
- $publicKeyResource = openssl_pkey_get_public($publicKeyPem);
- if (!$publicKeyResource) {
- throw new InvalidArgumentException('公钥格式错误: ' . openssl_error_string());
- }
- // 解码Base64签名
- $signature = base64_decode($signature);
- // 验证签名(SHA256withRSA)
- $result = openssl_verify($data, $signature, $publicKeyResource, OPENSSL_ALGO_SHA256);
- // 释放资源
- openssl_free_key($publicKeyResource);
- // 返回验证结果(1=成功,0=失败,-1=错误)
- if ($result === 1) {
- return true;
- } elseif ($result === 0) {
- return false;
- } else {
- throw new RuntimeException('验签过程发生错误: ' . openssl_error_string());
- }
- }
- }
|