Rsa.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. * @param string $publicKey 公钥
  25. * @param string $privateKey 私钥
  26. */
  27. public function __construct($publicKey = null, $privateKey = null)
  28. {
  29. $this->setKey($publicKey, $privateKey);
  30. }
  31. /**
  32. * 设置公钥和私钥
  33. * @param string $publicKey 公钥
  34. * @param string $privateKey 私钥
  35. */
  36. public function setKey($publicKey = null, $privateKey = null)
  37. {
  38. if (!is_null($publicKey)) {
  39. $this->publicKey = $publicKey;
  40. }
  41. if (!is_null($privateKey)) {
  42. $this->privateKey = $privateKey;
  43. }
  44. }
  45. /**
  46. * * setup the private key
  47. */
  48. private function setupPrivKey()
  49. {
  50. if (is_resource($this->_privKey)) {
  51. return true;
  52. }
  53. $pem = chunk_split($this->privateKey, 64, "\n");
  54. $pem = "-----BEGIN PRIVATE KEY-----\n" . $pem . "-----END PRIVATE KEY-----\n";
  55. $this->_privKey = openssl_pkey_get_private($pem);
  56. return true;
  57. }
  58. /**
  59. * * setup the public key
  60. */
  61. private function setupPubKey()
  62. {
  63. if (is_resource($this->_pubKey)) {
  64. return true;
  65. }
  66. $pem = chunk_split($this->publicKey, 64, "\n");
  67. $pem = "-----BEGIN PUBLIC KEY-----\n" . $pem . "-----END PUBLIC KEY-----\n";
  68. $this->_pubKey = openssl_pkey_get_public($pem);
  69. return true;
  70. }
  71. /**
  72. * * encrypt with the private key
  73. */
  74. public function privEncrypt($data)
  75. {
  76. if (!is_string($data)) {
  77. return null;
  78. }
  79. $this->setupPrivKey();
  80. $r = openssl_private_encrypt($data, $encrypted, $this->_privKey);
  81. if ($r) {
  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. return null;
  93. }
  94. $this->setupPrivKey();
  95. $encrypted = base64_decode($encrypted);
  96. $r = openssl_private_decrypt($encrypted, $decrypted, $this->_privKey);
  97. if ($r) {
  98. return $decrypted;
  99. }
  100. return null;
  101. }
  102. /**
  103. * * encrypt with public key
  104. */
  105. public function pubEncrypt($data)
  106. {
  107. if (!is_string($data)) {
  108. return null;
  109. }
  110. $this->setupPubKey();
  111. $r = openssl_public_encrypt($data, $encrypted, $this->_pubKey);
  112. if ($r) {
  113. return base64_encode($encrypted);
  114. }
  115. return null;
  116. }
  117. /**
  118. * * decrypt with the public key
  119. */
  120. public function pubDecrypt($crypted)
  121. {
  122. if (!is_string($crypted)) {
  123. return null;
  124. }
  125. $this->setupPubKey();
  126. $crypted = base64_decode($crypted);
  127. $r = openssl_public_decrypt($crypted, $decrypted, $this->_pubKey);
  128. if ($r) {
  129. return $decrypted;
  130. }
  131. return null;
  132. }
  133. /**
  134. * 构造签名
  135. * @param string $dataString 被签名数据
  136. * @return string
  137. */
  138. public function sign($dataString)
  139. {
  140. $this->setupPrivKey();
  141. $signature = false;
  142. openssl_sign($dataString, $signature, $this->_privKey);
  143. return base64_encode($signature);
  144. }
  145. /**
  146. * 验证签名
  147. * @param string $dataString 被签名数据
  148. * @param string $signString 已经签名的字符串
  149. * @return number 1签名正确 0签名错误
  150. */
  151. public function verify($dataString, $signString)
  152. {
  153. $this->setupPubKey();
  154. $signature = base64_decode($signString);
  155. $flg = openssl_verify($dataString, $signature, $this->_pubKey);
  156. return $flg;
  157. }
  158. public function __destruct()
  159. {
  160. is_resource($this->_privKey) && @openssl_free_key($this->_privKey);
  161. is_resource($this->_pubKey) && @openssl_free_key($this->_pubKey);
  162. }
  163. }