Rsa.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace crypt;
  3. class Rsa {
  4. /**
  5. * @uses 获取私钥
  6. * @return bool|resource
  7. */
  8. private static function getPrivateKey()
  9. {
  10. $abs_path = dirname(__FILE__) . '/file/rsa_private_key.pem';
  11. $content = file_get_contents($abs_path);
  12. if (empty($content)) return false;
  13. $res = "-----BEGIN RSA PRIVATE KEY-----\n" .
  14. wordwrap($content, 64, "\n", true) .
  15. "\n-----END RSA PRIVATE KEY-----";
  16. return openssl_pkey_get_private($res);
  17. }
  18. /**
  19. * @uses 获取公钥
  20. * @return bool|resource
  21. */
  22. private static function getPublicKey()
  23. {
  24. $abs_path = dirname(__FILE__) . '/file/rsa_public_key.pem';
  25. $content = file_get_contents($abs_path);
  26. if (empty($content)) return false;
  27. $res = "-----BEGIN PUBLIC KEY-----\n" .
  28. wordwrap($content, 64, "\n", true) .
  29. "\n-----END PUBLIC KEY-----";
  30. return openssl_pkey_get_public($res);
  31. }
  32. private static function getKenLen() {
  33. $pub_id = openssl_get_publickey(self::getPublicKey());
  34. return openssl_pkey_get_details($pub_id)['bits'];
  35. }
  36. public static function getPK(){
  37. return self::getPublicKey();
  38. }
  39. /**
  40. * @uses 私钥加密
  41. * @param string $data
  42. * @return null|string
  43. */
  44. public static function privEncrypt($data = '')
  45. {
  46. if (!is_string($data)) return null;
  47. $crypto = '';
  48. $encrypted = '';
  49. $len = self::getKenLen() / 8 - 11;
  50. foreach (str_split($data, $len) as $chunk) {
  51. openssl_private_encrypt($chunk, $encrypted, self::getPrivateKey());
  52. $crypto .= $encrypted;
  53. }
  54. return base64_encode($crypto);
  55. }
  56. /**
  57. * @uses 公钥加密
  58. * @param string $data
  59. * @return null|string
  60. */
  61. public static function publicEncrypt($data = '')
  62. {
  63. if (!is_string($data)) return null;
  64. $crypto = '';
  65. $encrypted = '';
  66. $len = self::getKenLen() / 8 - 11;
  67. foreach (str_split($data, $len) as $chunk) {
  68. openssl_public_encrypt($chunk, $encrypted, self::getPublicKey());
  69. $crypto .= $encrypted;
  70. }
  71. return base64_encode($crypto);
  72. }
  73. /**
  74. * @uses 私钥解密
  75. * @param string $encrypted
  76. * @return null
  77. */
  78. public static function privDecrypt($encrypted = '')
  79. {
  80. if (!is_string($encrypted)) return null;
  81. $crypto = '';
  82. $len = self::getKenLen() / 8;
  83. foreach (str_split(base64_decode($encrypted), $len) as $chunk) {
  84. $decryptData = '';
  85. openssl_private_decrypt($chunk, $decryptData, self::getPrivateKey());
  86. $crypto .= $decryptData;
  87. }
  88. return $crypto;
  89. }
  90. /**
  91. * @uses 公钥解密
  92. * @param string $encrypted
  93. * @return null
  94. */
  95. public static function publicDecrypt($encrypted = '')
  96. {
  97. if (!is_string($encrypted)) return null;
  98. $crypto = '';
  99. $decrypted = '';
  100. $len = self::getKenLen() / 8;
  101. foreach (str_split(base64_decode($encrypted), $len) as $chunk) {
  102. openssl_public_decrypt($chunk, $decrypted, self::getPublicKey());
  103. $crypto .= $decrypted;
  104. }
  105. return $crypto;
  106. }
  107. }