RsaUtil.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace app\utils;
  3. /**
  4. * RSA 加密工具类
  5. */
  6. class RsaUtil{
  7. const RSA_ALGORITHM_KEY_TYPE = OPENSSL_KEYTYPE_RSA;
  8. const RSA_ALGORITHM_SIGN = OPENSSL_ALGO_SHA256;
  9. protected $public_key; // 公钥
  10. protected $private_key; // 私钥
  11. protected $key_len;
  12. /**
  13. * @throws \Exception
  14. */
  15. public function __construct($pub_key = '', $pri_key = '')
  16. {
  17. $pub_key = file_get_contents(!empty($pub_key) ? $pub_key : APP_PATH.'/utils/certs/public_key.pem'); // 获取公钥文件中的数据
  18. $pri_key = file_get_contents(!empty($pri_key) ? $pri_key : APP_PATH.'/utils/certs/private_key.pem'); // 获取私钥文件中的数据
  19. if ($pub_key) {
  20. $this->public_key = $pub_key;
  21. $pub_id = openssl_pkey_get_public($this->public_key);
  22. $this->key_len = openssl_pkey_get_details($pub_id)['bits'];
  23. }
  24. if ($pri_key) {
  25. $this->private_key = $pri_key;
  26. $pri_id = openssl_pkey_get_private($this->private_key);
  27. $this->key_len = openssl_pkey_get_details($pri_id)['bits'];
  28. }
  29. }
  30. /**
  31. * 创建密钥对
  32. *
  33. * @param $key_size
  34. * @return array
  35. */
  36. public static function createKeys($key_size = 1024)
  37. {
  38. $config = array(
  39. "private_key_bits" => $key_size,
  40. "private_key_type" => self::RSA_ALGORITHM_KEY_TYPE,
  41. );
  42. $res = openssl_pkey_new($config);
  43. openssl_pkey_export($res, $private_key);
  44. $public_key_detail = openssl_pkey_get_details($res);
  45. $public_key = $public_key_detail["key"];
  46. return array(
  47. "public_key" => $public_key,
  48. "private_key" => $private_key,
  49. );
  50. }
  51. /**
  52. * 私钥解密
  53. *
  54. * @param $encrypted
  55. * @return string
  56. */
  57. public function privateDecrypt($encrypted)
  58. {
  59. $decrypted = "";
  60. $part_len = $this->key_len / 8;
  61. //url 中的get传值默认会吧+号过滤成' ',替换回来就好了
  62. str_replace('% ', '+', $encrypted);
  63. $base64_decoded = base64_decode($encrypted);
  64. $parts = str_split($base64_decoded, $part_len);
  65. foreach ($parts as $part) {
  66. $decrypted_temp = '';
  67. openssl_private_decrypt($part, $decrypted_temp, $this->private_key);
  68. $decrypted .= $decrypted_temp;
  69. }
  70. return $decrypted;
  71. }
  72. /**
  73. * 私钥加密
  74. *
  75. * @param $data
  76. * @return string
  77. */
  78. public function privateEncrypt($data)
  79. {
  80. $encrypted = '';
  81. $part_len = $this->key_len / 8 - 11;
  82. $parts = str_split($data, $part_len);
  83. foreach ($parts as $part) {
  84. $encrypted_temp = '';
  85. openssl_private_encrypt($part, $encrypted_temp, $this->private_key);
  86. $encrypted .= $encrypted_temp;
  87. }
  88. return base64_encode($encrypted);
  89. }
  90. /**
  91. * 公钥解密
  92. *
  93. * @param $encrypted
  94. * @return string
  95. */
  96. public function publicDecrypt($encrypted)
  97. {
  98. $decrypted = "";
  99. $part_len = $this->key_len / 8;
  100. $base64_decoded = base64_decode($encrypted);
  101. $parts = str_split($base64_decoded, $part_len);
  102. foreach ($parts as $part) {
  103. $decrypted_temp = '';
  104. openssl_public_decrypt($part, $decrypted_temp, $this->public_key);
  105. $decrypted .= $decrypted_temp;
  106. }
  107. return $decrypted;
  108. }
  109. /**
  110. * 公钥加密
  111. *
  112. * @param $data
  113. * @return string
  114. */
  115. public function publicEncrypt($data)
  116. {
  117. $encrypted = '';
  118. $part_len = $this->key_len / 8 - 11;
  119. $parts = str_split($data, $part_len);
  120. foreach ($parts as $part) {
  121. $encrypted_temp = '';
  122. openssl_public_encrypt($part, $encrypted_temp, $this->public_key);
  123. $encrypted .= $encrypted_temp;
  124. }
  125. return base64_encode($encrypted);
  126. }
  127. /**
  128. * 数据加签
  129. *
  130. * @param $data
  131. * @return string
  132. */
  133. public function sign($data)
  134. {
  135. openssl_sign($data, $sign, $this->private_key, self::RSA_ALGORITHM_SIGN);
  136. return base64_encode($sign);
  137. }
  138. /**
  139. * 数据签名验证
  140. *
  141. * @param $data
  142. * @param $sign
  143. * @return false|int
  144. */
  145. public function verify($data, $sign)
  146. {
  147. $pub_id = openssl_get_publickey($this->public_key);
  148. $res = openssl_verify($data, base64_decode($sign), $pub_id, self::RSA_ALGORITHM_SIGN);
  149. return $res;
  150. }
  151. }