Prpcrypt.Class.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace JinDouYun\Controller;
  3. /**
  4. * PHP7.1及其之上版本的回调加解密类库
  5. * 该版本依赖openssl_encrypt方法加解密,注意版本依赖 (PHP 5 >= 5.3.0, PHP 7)
  6. */
  7. class Prpcrypt
  8. {
  9. public $key;
  10. function __construct($k)
  11. {
  12. $this->key = base64_decode($k . "=");
  13. }
  14. function Prpcrypt($k)
  15. {
  16. $this->key = base64_decode($k . "=");
  17. }
  18. public function encrypt($text, $corpid)
  19. {
  20. try {
  21. //获得16位随机字符串,填充到明文之前
  22. $random = $this->getRandomStr();
  23. $text = $random . pack("N", strlen($text)) . $text . $corpid;
  24. $iv = substr($this->key, 0, 16);
  25. // 网络字节序
  26. // $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
  27. // $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  28. //使用自定义的填充方式对明文进行补位填充
  29. $pkc_encoder = new PKCS7Encoder;
  30. $text = $pkc_encoder->encode($text);
  31. // mcrypt_generic_init($module, $this->key, $iv);
  32. // //加密
  33. // $encrypted = mcrypt_generic($module, $text);
  34. // mcrypt_generic_deinit($module);
  35. // mcrypt_module_close($module);
  36. $encrypted = openssl_encrypt($text, 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv );
  37. //print(base64_encode($encrypted));
  38. //使用BASE64对加密后的字符串进行编码
  39. return array(ErrorCode::$OK, base64_encode($encrypted));
  40. } catch (\Exception $e) {
  41. print $e;
  42. return array(ErrorCode::$EncryptAESError, null);
  43. }
  44. }
  45. public function decrypt($encrypted, $corpid)
  46. {
  47. try {
  48. $ciphertext_dec = base64_decode($encrypted);
  49. // $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  50. $iv = substr($this->key, 0, 16);
  51. // mcrypt_generic_init($module, $this->key, $iv);
  52. // $decrypted = mdecrypt_generic($module, $ciphertext_dec);
  53. // mcrypt_generic_deinit($module);
  54. // mcrypt_module_close($module);
  55. $decrypted = openssl_decrypt ( $ciphertext_dec, 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv );
  56. // return $decrypted;
  57. } catch (\Exception $e) {
  58. return array(ErrorCode::$DecryptAESError, null);
  59. }
  60. try {
  61. //去除补位字符
  62. $pkc_encoder = new PKCS7Encoder;
  63. $result = $pkc_encoder->decode($decrypted);
  64. //去除16位随机字符串,网络字节序和AppId
  65. if (strlen($result) < 16)
  66. return "";
  67. $content = substr($result, 16, strlen($result));
  68. $len_list = unpack("N", substr($content, 0, 4));
  69. $xml_len = $len_list[1];
  70. $xml_content = substr($content, 4, $xml_len);
  71. $from_corpid = substr($content, $xml_len + 4);
  72. } catch (\Exception $e) {
  73. print $e;
  74. return array(ErrorCode::$DecryptAESError, null);
  75. }
  76. if ($from_corpid != $corpid)
  77. return array(ErrorCode::$ValidateSuiteKeyError, null);
  78. return array(0, $xml_content);
  79. }
  80. function getRandomStr()
  81. {
  82. $str = "";
  83. $str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
  84. $max = strlen($str_pol) - 1;
  85. for ($i = 0; $i < 16; $i++) {
  86. $str .= $str_pol[mt_rand(0, $max)];
  87. }
  88. return $str;
  89. }
  90. }
  91. ?>