DingTalkCrypto.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace App\Service;
  3. class DingTalkCrypto
  4. {
  5. protected string $token;
  6. protected string $encodingAesKey; // URL safe base64 字符串,长度 43
  7. public function __construct(string $token, string $encodingAesKey)
  8. {
  9. $this->token = $token;
  10. $this->encodingAesKey = $encodingAesKey;
  11. }
  12. /**
  13. * 解密钉钉事件回调消息
  14. *
  15. * @param string $signature URL参数 signature
  16. * @param string $timestamp URL参数 timestamp
  17. * @param string $nonce URL参数 nonce
  18. * @param string $encrypt 消息体 encrypt
  19. * @return array|false
  20. */
  21. public function decryptMsg(string $msgSignature, string $timestamp, string $nonce, string $encrypt)
  22. {
  23. // 验签
  24. $tmpArr = [$this->token, $timestamp, $nonce, $encrypt];
  25. sort($tmpArr, SORT_STRING);
  26. $tmpStr = implode('', $tmpArr);
  27. $hash = sha1($tmpStr);
  28. if ($hash !== $msgSignature) {
  29. return false;
  30. }
  31. // AES 解密
  32. $aesKey = base64_decode($this->encodingAesKey . '='); // 补足 padding
  33. $iv = substr($aesKey, 0, 16);
  34. $cipherText = base64_decode($encrypt);
  35. $decrypted = openssl_decrypt($cipherText, 'AES-256-CBC', $aesKey, OPENSSL_ZERO_PADDING, $iv);
  36. if ($decrypted === false) return false;
  37. // 去掉 padding
  38. $decrypted = $this->pkcs7Unpad($decrypted);
  39. // 去掉前16位随机字符串
  40. $content = substr($decrypted, 16);
  41. // 读取消息长度
  42. $lenList = unpack("N", substr($content, 0, 4));
  43. $jsonLen = $lenList[1];
  44. // 获取 JSON
  45. $json = substr($content, 4, $jsonLen);
  46. return json_decode($json, true);
  47. }
  48. /**
  49. * 加密返回给钉钉的消息
  50. *
  51. * @param string $replyMsg 明文消息
  52. * @param string $timestamp
  53. * @param string $nonce
  54. * @return array
  55. */
  56. public function encryptMsg(string $replyMsg, string $timestamp, string $nonce): array
  57. {
  58. $random16 = $this->getRandomStr(16);
  59. $msgLenBin = pack("N", strlen($replyMsg));
  60. $corpId = ''; // 不需要corpId
  61. $plain = $random16 . $msgLenBin . $replyMsg . $corpId;
  62. // PKCS#7 padding
  63. $blockSize = 32;
  64. $pad = $blockSize - (strlen($plain) % $blockSize);
  65. $pad = $pad === 0 ? $blockSize : $pad;
  66. $plainPadded = $plain . str_repeat(chr($pad), $pad);
  67. // AES 加密
  68. $aesKey = base64_decode($this->encodingAesKey);
  69. $iv = substr($aesKey, 0, 16);
  70. $encrypted = openssl_encrypt($plainPadded, 'AES-256-CBC', $aesKey, OPENSSL_ZERO_PADDING, $iv);
  71. $encryptBase64 = base64_encode($encrypted);
  72. // 计算 msg_signature
  73. $signatureStr = $this->token . $timestamp . $nonce . $encryptBase64;
  74. $msgSignature = sha1($signatureStr);
  75. return [
  76. 'msg_signature' => $msgSignature,
  77. 'encrypt' => $encryptBase64
  78. ];
  79. }
  80. private function getRandomStr(int $length = 16): string
  81. {
  82. $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  83. $str = '';
  84. for ($i = 0; $i < $length; $i++) {
  85. $str .= $chars[random_int(0, strlen($chars) - 1)];
  86. }
  87. return $str;
  88. }
  89. private function pkcs7Unpad(string $text)
  90. {
  91. $pad = ord(substr($text, -1));
  92. if ($pad < 1 || $pad > 32) return false;
  93. return substr($text, 0, -$pad);
  94. }
  95. }