DingTalkCrypto.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 $msgSignature URL参数 msg_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. // 1. 验签 (官方要求 token + timestamp + nonce + encrypt 顺序拼接)
  24. $hash = sha1($this->token . $timestamp . $nonce . $encrypt);
  25. if ($hash !== $msgSignature) {
  26. return false;
  27. }
  28. // 2. AES 解密
  29. $aesKey = base64_decode($this->encodingAesKey);
  30. $iv = substr($aesKey, 0, 16);
  31. $cipherText = base64_decode($encrypt);
  32. $decrypted = openssl_decrypt($cipherText, 'AES-256-CBC', $aesKey, OPENSSL_ZERO_PADDING, $iv);
  33. if ($decrypted === false) return false;
  34. // 3. 去掉 PKCS#7 padding
  35. $decrypted = $this->pkcs7Unpad($decrypted);
  36. // 4. 去掉前16位随机字符串
  37. $content = substr($decrypted, 16);
  38. // 5. 读取消息长度(4字节 network order)
  39. $lenList = unpack("N", substr($content, 0, 4));
  40. $jsonLen = $lenList[1];
  41. // 6. 获取 JSON
  42. $json = substr($content, 4, $jsonLen);
  43. return json_decode($json, true);
  44. }
  45. /**
  46. * 加密返回给钉钉的消息
  47. *
  48. * @param string $replyMsg 明文消息
  49. * @param string $timestamp
  50. * @param string $nonce
  51. * @return array
  52. */
  53. public function encryptMsg(string $replyMsg, string $timestamp, string $nonce): array
  54. {
  55. $random16 = $this->getRandomStr(16);
  56. $msgLenBin = pack("N", strlen($replyMsg));
  57. $corpId = ''; // 不需要 corpId
  58. $plain = $random16 . $msgLenBin . $replyMsg . $corpId;
  59. // PKCS#7 padding
  60. $blockSize = 32;
  61. $pad = $blockSize - (strlen($plain) % $blockSize);
  62. $pad = $pad === 0 ? $blockSize : $pad;
  63. $plainPadded = $plain . str_repeat(chr($pad), $pad);
  64. // AES 加密
  65. $aesKey = base64_decode($this->encodingAesKey);
  66. $iv = substr($aesKey, 0, 16);
  67. $encrypted = openssl_encrypt($plainPadded, 'AES-256-CBC', $aesKey, OPENSSL_ZERO_PADDING, $iv);
  68. $encryptBase64 = base64_encode($encrypted);
  69. // 计算 msg_signature
  70. $msgSignature = sha1($this->token . $timestamp . $nonce . $encryptBase64);
  71. return [
  72. 'msg_signature' => $msgSignature,
  73. 'encrypt' => $encryptBase64
  74. ];
  75. }
  76. private function getRandomStr(int $length = 16): string
  77. {
  78. $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  79. $str = '';
  80. for ($i = 0; $i < $length; $i++) {
  81. $str .= $chars[random_int(0, strlen($chars) - 1)];
  82. }
  83. return $str;
  84. }
  85. private function pkcs7Unpad(string $text)
  86. {
  87. $pad = ord(substr($text, -1));
  88. if ($pad < 1 || $pad > 32) return false;
  89. return substr($text, 0, -$pad);
  90. }
  91. }