Rsa.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace fast;
  3. /**
  4. * RSA签名类
  5. */
  6. class Rsa
  7. {
  8. public $publicKey = '';
  9. public $privateKey = '';
  10. private $_privKey;
  11. /**
  12. * * private key
  13. */
  14. private $_pubKey;
  15. /**
  16. * * public key
  17. */
  18. private $_keyPath;
  19. /**
  20. * * the keys saving path
  21. */
  22. /**
  23. * * the construtor,the param $path is the keys saving path
  24. */
  25. function __construct($publicKey = null, $privateKey = null)
  26. {
  27. $this->setKey($publicKey, $privateKey);
  28. }
  29. /**
  30. * 设置公钥和私钥
  31. * @param string $publicKey 公钥
  32. * @param string $privateKey 私钥
  33. */
  34. public function setKey($publicKey = null, $privateKey = null)
  35. {
  36. if (!is_null($publicKey))
  37. $this->publicKey = $publicKey;
  38. if (!is_null($privateKey))
  39. $this->privateKey = $privateKey;
  40. }
  41. /**
  42. * * setup the private key
  43. */
  44. private function setupPrivKey()
  45. {
  46. if (is_resource($this->_privKey))
  47. {
  48. return true;
  49. }
  50. $pem = chunk_split($this->privateKey, 64, "\n");
  51. $pem = "-----BEGIN PRIVATE KEY-----\n" . $pem . "-----END PRIVATE KEY-----\n";
  52. $this->_privKey = openssl_pkey_get_private($pem);
  53. return true;
  54. }
  55. /**
  56. * * setup the public key
  57. */
  58. private function setupPubKey()
  59. {
  60. if (is_resource($this->_pubKey))
  61. {
  62. return true;
  63. }
  64. $pem = chunk_split($this->publicKey, 64, "\n");
  65. $pem = "-----BEGIN PUBLIC KEY-----\n" . $pem . "-----END PUBLIC KEY-----\n";
  66. $this->_pubKey = openssl_pkey_get_public($pem);
  67. return true;
  68. }
  69. /**
  70. * * encrypt with the private key
  71. */
  72. public function privEncrypt($data)
  73. {
  74. if (!is_string($data))
  75. {
  76. return null;
  77. }
  78. $this->setupPrivKey();
  79. $r = openssl_private_encrypt($data, $encrypted, $this->_privKey);
  80. if ($r)
  81. {
  82. return base64_encode($encrypted);
  83. }
  84. return null;
  85. }
  86. /**
  87. * * decrypt with the private key
  88. */
  89. public function privDecrypt($encrypted)
  90. {
  91. if (!is_string($encrypted))
  92. {
  93. return null;
  94. }
  95. $this->setupPrivKey();
  96. $encrypted = base64_decode($encrypted);
  97. $r = openssl_private_decrypt($encrypted, $decrypted, $this->_privKey);
  98. if ($r)
  99. {
  100. return $decrypted;
  101. }
  102. return null;
  103. }
  104. /**
  105. * * encrypt with public key
  106. */
  107. public function pubEncrypt($data)
  108. {
  109. if (!is_string($data))
  110. {
  111. return null;
  112. }
  113. $this->setupPubKey();
  114. $r = openssl_public_encrypt($data, $encrypted, $this->_pubKey);
  115. if ($r)
  116. {
  117. return base64_encode($encrypted);
  118. }
  119. return null;
  120. }
  121. /**
  122. * * decrypt with the public key
  123. */
  124. public function pubDecrypt($crypted)
  125. {
  126. if (!is_string($crypted))
  127. {
  128. return null;
  129. }
  130. $this->setupPubKey();
  131. $crypted = base64_decode($crypted);
  132. $r = openssl_public_decrypt($crypted, $decrypted, $this->_pubKey);
  133. if ($r)
  134. {
  135. return $decrypted;
  136. }
  137. return null;
  138. }
  139. /**
  140. * 构造签名
  141. * @param string $dataString 被签名数据
  142. * @return string
  143. */
  144. public function sign($dataString)
  145. {
  146. $this->setupPrivKey();
  147. $signature = false;
  148. openssl_sign($dataString, $signature, $this->_privKey);
  149. return base64_encode($signature);
  150. }
  151. /**
  152. * 验证签名
  153. * @param string $dataString 被签名数据
  154. * @param string $signString 已经签名的字符串
  155. * @return number 1签名正确 0签名错误
  156. */
  157. public function verify($dataString, $signString)
  158. {
  159. $this->setupPubKey();
  160. $signature = base64_decode($signString);
  161. $flg = openssl_verify($dataString, $signature, $this->_pubKey);
  162. return $flg;
  163. }
  164. public function __destruct()
  165. {
  166. is_resource($this->_privKey) && @openssl_free_key($this->_privKey);
  167. is_resource($this->_pubKey) && @openssl_free_key($this->_pubKey);
  168. }
  169. }