Utils.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace app\extra\jhfPay;
  3. use InvalidArgumentException;
  4. use RuntimeException;
  5. class Utils
  6. {
  7. /**
  8. * AES加密函数(ECB模式,32位密钥)
  9. *
  10. * @param string $plaintext 待加密的明文
  11. * @param string $key 32字节(256位)密钥
  12. * @return string 加密后的Base64编码字符串
  13. */
  14. public static function aes_encrypt($plaintext, $key) {
  15. // 验证密钥长度(32字节 = 256位)
  16. if (strlen($key) !== 32) {
  17. throw new InvalidArgumentException('密钥长度必须为32字节');
  18. }
  19. // 执行加密(ECB模式,PKCS#7填充)
  20. $ciphertext = openssl_encrypt(
  21. $plaintext,
  22. 'AES-256-ECB',
  23. $key,
  24. OPENSSL_RAW_DATA
  25. );
  26. // 返回Base64编码结果
  27. return base64_encode($ciphertext);
  28. }
  29. /**
  30. * AES解密函数(ECB模式,32位密钥)
  31. *
  32. * @param string $ciphertext 待解密的Base64编码密文
  33. * @param string $key 32字节(256位)密钥
  34. * @return string 解密后的明文
  35. */
  36. public static function aes_decrypt($ciphertext, $key) {
  37. // 验证密钥长度(32字节 = 256位)
  38. if (strlen($key) !== 32) {
  39. throw new InvalidArgumentException('密钥长度必须为32字节');
  40. }
  41. // 解码Base64密文
  42. $ciphertext = base64_decode($ciphertext);
  43. // 执行解密(ECB模式,PKCS#7填充)
  44. return openssl_decrypt(
  45. $ciphertext,
  46. 'AES-256-ECB',
  47. $key,
  48. OPENSSL_RAW_DATA
  49. );
  50. }
  51. /**
  52. * RSA签名函数(SHA256withRSA,1024位密钥)
  53. *
  54. * @param string $data 待签名的数据
  55. * @param string $privateKeyText 私钥纯文本内容(不带PEM头和尾)
  56. * @return string 签名后的Base64编码字符串
  57. */
  58. public static function rsa_sign(string $data, string $privateKeyText): string
  59. {
  60. // 将纯文本私钥转换为PEM格式
  61. $privateKeyPem = "-----BEGIN PRIVATE KEY-----\n" .
  62. chunk_split($privateKeyText, 64, "\n") .
  63. "-----END PRIVATE KEY-----";
  64. // 创建私钥资源
  65. $privateKeyResource = openssl_pkey_get_private($privateKeyPem);
  66. if (!$privateKeyResource) {
  67. throw new InvalidArgumentException('私钥格式错误: ' . openssl_error_string());
  68. }
  69. // 生成签名(SHA256withRSA)
  70. $success = openssl_sign($data, $signature, $privateKeyResource, OPENSSL_ALGO_SHA256);
  71. // 释放资源
  72. unset($privateKey);
  73. // openssl_free_key($privateKeyResource);
  74. if (!$success) {
  75. throw new RuntimeException('签名失败: ' . openssl_error_string());
  76. }
  77. // 返回Base64编码的签名
  78. return base64_encode($signature);
  79. }
  80. /**
  81. * RSA验签函数(SHA256withRSA,1024位密钥)
  82. *
  83. * @param string $data 原始数据
  84. * @param string $signature 签名的Base64编码字符串
  85. * @param string $publicKeyText 公钥纯文本内容(不带PEM头和尾)
  86. * @return bool 验签结果
  87. */
  88. public static function rsa_verify(string $data, string $signature, string $publicKeyText): bool
  89. {
  90. // 将纯文本公钥转换为PEM格式
  91. $publicKeyPem = "-----BEGIN PUBLIC KEY-----\n" .
  92. chunk_split($publicKeyText, 64, "\n") .
  93. "-----END PUBLIC KEY-----";
  94. // 创建公钥资源
  95. $publicKeyResource = openssl_pkey_get_public($publicKeyPem);
  96. if (!$publicKeyResource) {
  97. throw new InvalidArgumentException('公钥格式错误: ' . openssl_error_string());
  98. }
  99. // 解码Base64签名
  100. $signature = base64_decode($signature);
  101. // 验证签名(SHA256withRSA)
  102. $result = openssl_verify($data, $signature, $publicKeyResource, OPENSSL_ALGO_SHA256);
  103. // 释放资源
  104. openssl_free_key($publicKeyResource);
  105. // 返回验证结果(1=成功,0=失败,-1=错误)
  106. if ($result === 1) {
  107. return true;
  108. } elseif ($result === 0) {
  109. return false;
  110. } else {
  111. throw new RuntimeException('验签过程发生错误: ' . openssl_error_string());
  112. }
  113. }
  114. }