Rsa.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: yckj_lzj
  5. * Date: 2019-10-22
  6. * Time: 19:27
  7. */
  8. namespace app\common\util;
  9. class Rsa extends SingleObjectClass
  10. {
  11. protected $priKeyStr;
  12. protected $pubKeyStr;
  13. public function setPriPubKey($pubKey,$priKey){
  14. $this->priKeyStr=$priKey;
  15. $this->pubKeyStr=$pubKey;
  16. // $this->$pubKeyStr=$pubKey;
  17. }
  18. public function setPriKey($priKey){
  19. $this->priKeyStr=$priKey;
  20. }
  21. public function setPubKey($pubKey){
  22. $this->pubKeyStr=$pubKey;
  23. }
  24. private function getPriKeyStr()
  25. {
  26. return "-----BEGIN PRIVATE KEY-----\n".$this->priKeyStr."\n-----END PRIVATE KEY-----";
  27. }
  28. public function getPubKeyStr(){
  29. return "-----BEGIN PUBLIC KEY-----\n".$this->pubKeyStr."\n-----END PUBLIC KEY-----";
  30. }
  31. /**
  32. * 获取公钥 *
  33. * @return bool|resource
  34. */
  35. private function getPublicKey()
  36. {
  37. $content = $this->getPubKeyStr();
  38. return openssl_pkey_get_public($content);
  39. }
  40. /**
  41. * 获取私钥
  42. * @return bool|resource
  43. */
  44. private function getPrivateKey()
  45. {
  46. $content = $this->getPriKeyStr();
  47. // echo $content;
  48. return openssl_pkey_get_private($content);
  49. }
  50. /**
  51. * 私钥加密
  52. * @param $signString
  53. * @return string
  54. */
  55. public function priSign($signString){
  56. $privKeyId = $this->getPrivateKey();
  57. $signature = '';
  58. openssl_sign($signString, $signature, $privKeyId);
  59. openssl_free_key($privKeyId);
  60. return base64_encode($signature);
  61. }
  62. /**
  63. * 私钥加密
  64. * @param string $data
  65. * @return null|string
  66. */
  67. public function privEncrypt($data = '')
  68. {
  69. if (!is_string($data)) {
  70. return null;
  71. }
  72. return openssl_private_encrypt($data, $encrypted, $this->getPrivateKey()) ? base64_encode($encrypted) : null;
  73. }
  74. /**
  75. * 公钥加密
  76. * @param string $data
  77. * @return null|string
  78. */
  79. public function publicEncrypt($data = '')
  80. {
  81. if (!is_string($data)) {
  82. return null;
  83. }
  84. var_dump($this->getPublicKey());
  85. return openssl_public_encrypt($data, $encrypted, $this->getPublicKey()) ? base64_encode($encrypted) : null;
  86. }
  87. /**
  88. * 私钥解密
  89. * @param string $encrypted
  90. * @return null
  91. */
  92. public function privDecrypt($encrypted = '')
  93. {
  94. if (!is_string($encrypted)) {
  95. return null;
  96. }
  97. return (openssl_private_decrypt(base64_decode($encrypted), $decrypted, $this->getPrivateKey())) ? $decrypted : null;
  98. }
  99. /**
  100. * 公钥解密
  101. * @param string $encrypted
  102. * @return null
  103. */
  104. public function publicDecrypt($encrypted = '')
  105. {
  106. if (!is_string($encrypted)) {
  107. return null;
  108. }
  109. return (openssl_public_decrypt(base64_decode($encrypted), $decrypted, $this->getPublicKey())) ? $decrypted : null;
  110. }
  111. }