EncryptUtil.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: blank
  5. * Date: 2020/4/23
  6. * Time: 9:59
  7. */
  8. namespace app\common\services\utils;
  9. use app\common\exceptions\ShopException;
  10. /**
  11. * 加解密工具类
  12. * Class EncryptUtil
  13. * @package app\common\services\utils
  14. */
  15. class EncryptUtil
  16. {
  17. const AES_EBC_MODE = "AES-128-ECB";
  18. /**
  19. * sha256签名
  20. * @param $data string 待签名字符串
  21. * @param $appSecret string 签名密码
  22. * @param bool $isBinary bool 返回二进制结果
  23. * @return string
  24. */
  25. public static function hash256($data, $secret, $isBinary = false)
  26. {
  27. $sign = hash_hmac('sha256', $data, $secret,$isBinary);
  28. return $sign;
  29. }
  30. /**
  31. * AES加密,模式为:AES/ECB/PKCK7Padding
  32. * @param string $data
  33. * @param string $secKey
  34. * @param string $method
  35. * @return array
  36. */
  37. public static function encryptECB($data, $secKey, $method = null)
  38. {
  39. if (is_null($method)) {
  40. $method = self::AES_EBC_MODE;
  41. }
  42. $encrypted = openssl_encrypt($data, $method, $secKey, OPENSSL_RAW_DATA);
  43. if($encrypted === false){
  44. return self::returnData(false,'aes加密失败');
  45. }
  46. return self::returnData(true,'aes加密', base64_encode($encrypted));
  47. }
  48. /**
  49. * AES解密,模式为:AES/ECB/PKCK7Padding
  50. * @param string $data
  51. * @param string $secKey
  52. * @param string $method
  53. * @return array
  54. */
  55. public static function decryptECB($data, $secKey, $method = null)
  56. {
  57. if (is_null($method)) {
  58. $method = self::AES_EBC_MODE;
  59. }
  60. $decrypted = openssl_decrypt(base64_decode($data), $method, $secKey, OPENSSL_RAW_DATA);
  61. if($decrypted === false){
  62. return self::returnData(false,'aes解密失败');
  63. }
  64. return self::returnData(true,'aes解密', $decrypted);
  65. }
  66. /**
  67. * 使用公钥加密
  68. * @param string $data
  69. * @param string $public_content
  70. * @return array
  71. */
  72. public static function encrypt($data, $public_content)
  73. {
  74. $res = "-----BEGIN PUBLIC KEY-----\n" .
  75. wordwrap($public_content, 64, "\n", true) .
  76. "\n-----END PUBLIC KEY-----";
  77. $pubKey = openssl_get_publickey($res);
  78. if($pubKey === false){
  79. return self::returnData(false,'rsa解密公钥无效');
  80. }
  81. $crypted = '';
  82. $isSuccess = openssl_public_encrypt($data, $crypted, $pubKey);
  83. openssl_free_key($pubKey);
  84. if($isSuccess == false){
  85. return self::returnData(false,'rsa加密失败');
  86. }
  87. return self::returnData(true,'rsa加密', base64_encode($crypted));
  88. }
  89. /**
  90. * 使用私钥解密
  91. * @param string $data
  92. * @param string $private_content
  93. * @return array
  94. */
  95. public static function decrypt($data, $private_content)
  96. {
  97. $res = "-----BEGIN RSA PRIVATE KEY-----\n" .
  98. wordwrap($private_content, 64, "\n", true) .
  99. "\n-----END RSA PRIVATE KEY-----";
  100. $priKey = openssl_get_privatekey($res);
  101. if($priKey === false){
  102. return self::returnData(false,'rsa解密私钥无效');
  103. }
  104. $decrypted = '';
  105. $isSuccess = openssl_private_decrypt(base64_decode($data), $decrypted, $priKey);
  106. openssl_free_key($priKey);
  107. if(!$isSuccess){
  108. return self::returnData(false,'rsa解密失败');
  109. }
  110. return self::returnData(true,'rsa解密成功', $decrypted);
  111. }
  112. /**
  113. * 使用私钥进行签名
  114. * @param string $data
  115. * @param string $private_content
  116. * @return array
  117. */
  118. public static function sign($data, $private_content)
  119. {
  120. $res = "-----BEGIN RSA PRIVATE KEY-----\n" .
  121. wordwrap($private_content, 64, "\n", true) .
  122. "\n-----END RSA PRIVATE KEY-----";
  123. $priKey = openssl_get_privatekey($res);
  124. if($priKey === false){
  125. return self::returnData(false,'rsa签名私钥无效');
  126. }
  127. $binary_signature = '';
  128. $isSuccess = openssl_sign($data, $binary_signature, $priKey, OPENSSL_ALGO_MD5);
  129. openssl_free_key($priKey);
  130. if(!$isSuccess) {
  131. return self::returnData(false,'rsa签名失败');
  132. }
  133. return self::returnData(true,'rsa签名成功',base64_encode($binary_signature));
  134. }
  135. /**
  136. * 使用公钥进行验签
  137. * @param string $signData 需要验证签名的数据
  138. * @param string $signParam 签名字符串
  139. * @param string $public_content
  140. * @return array
  141. */
  142. public static function verify($signData, $signParam, $public_content)
  143. {
  144. $res = "-----BEGIN PUBLIC KEY-----\n" .
  145. wordwrap($public_content, 64, "\n", true) .
  146. "\n-----END PUBLIC KEY-----";
  147. $pubKey = openssl_get_publickey($res);
  148. if($pubKey === false) {
  149. return self::returnData(false,'rsa验签公钥无效');
  150. }
  151. $signParam = base64_decode($signParam);
  152. $isMatch = openssl_verify($signData, $signParam, $pubKey, OPENSSL_ALGO_MD5) === 1;
  153. openssl_free_key($pubKey);
  154. return self::returnData($isMatch,'rsa验签');
  155. }
  156. /**
  157. * @param bool $code 状态 true|false
  158. * @param string $msg 说明
  159. * @param string $data 数据
  160. * @return array
  161. */
  162. protected static function returnData($code, $msg = '', $data = '')
  163. {
  164. return ['code'=> $code, 'msg'=> $msg, 'data' => $data];
  165. }
  166. }