RSAUtil.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace addons\JoinPay\common\logic\util;
  3. use addons\JoinPay\common\logic\exceptions\SDKException;
  4. /**
  5. * 使用RSA进行加密、解密、签名、验签
  6. * Class RSAUtil
  7. * @package utils
  8. */
  9. class RSAUtil
  10. {
  11. /**
  12. * 使用公钥加密
  13. * @param string $data
  14. * @param string $pubKey
  15. * @return string
  16. * @throws SDKException
  17. */
  18. public static function encrypt(string $data, string $pubKey){
  19. $pubKey = openssl_get_publickey($pubKey);
  20. if($pubKey === false){
  21. throw new SDKException(SDKException::BIZ_ERROR, "rsa解密公钥无效");
  22. }
  23. $crypted = '';
  24. $isSuccess = openssl_public_encrypt($data, $crypted, $pubKey);
  25. openssl_free_key($pubKey);
  26. if($isSuccess == false){
  27. throw new SDKException(SDKException::BIZ_ERROR, "rsa加密失败");
  28. }
  29. return base64_encode($crypted);
  30. }
  31. /**
  32. * 使用私钥解密
  33. * @param string $data
  34. * @param string $priKey
  35. * @return string
  36. * @throws SDKException
  37. */
  38. public static function decrypt(string $data, string $priKey){
  39. $priKey = openssl_get_privatekey($priKey);
  40. if($priKey === false){
  41. throw new SDKException(SDKException::BIZ_ERROR, "rsa解密私钥无效");
  42. }
  43. $decrypted = '';
  44. $isSuccess = openssl_private_decrypt(base64_decode($data), $decrypted, $priKey);
  45. openssl_free_key($priKey);
  46. if(! $isSuccess){
  47. throw new SDKException(SDKException::BIZ_ERROR, "rsa解密失败");
  48. }
  49. return $decrypted;
  50. }
  51. /**
  52. * 使用私钥进行签名
  53. * @param string $data
  54. * @param string $priKey
  55. * @return string
  56. * @throws SDKException
  57. */
  58. public static function sign(string $data, string $priKey){
  59. $priKey = openssl_get_privatekey($priKey);
  60. if($priKey === false){
  61. throw new SDKException(SDKException::BIZ_ERROR, "rsa签名私钥无效");
  62. }
  63. $binary_signature = '';
  64. $isSuccess = openssl_sign($data, $binary_signature, $priKey, OPENSSL_ALGO_MD5);
  65. openssl_free_key($priKey);
  66. if(! $isSuccess){
  67. throw new SDKException(SDKException::BIZ_ERROR, "rsa签名失败");
  68. }
  69. return base64_encode($binary_signature);
  70. }
  71. /**
  72. * 使用公钥进行验签
  73. * @param string $signData
  74. * @param string $signParam
  75. * @param string $pubKey
  76. * @return bool
  77. * @throws SDKException
  78. */
  79. public static function verify(string $signData, string $signParam, string $pubKey){
  80. $pubKey = openssl_get_publickey($pubKey);
  81. if($pubKey === false){
  82. throw new SDKException(SDKException::BIZ_ERROR, "rsa验签公钥无效");
  83. }
  84. $signParam = base64_decode($signParam);
  85. $isMatch = openssl_verify($signData, $signParam, $pubKey, OPENSSL_ALGO_MD5) === 1;
  86. openssl_free_key($pubKey);
  87. return $isMatch;
  88. }
  89. }