diff --git a/package.json b/package.json index 8648df5..76aaa6b 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "sync": "curl -X PUT 'https://npm.taobao.org/sync/coa-wx-work?sync_upstream=true'" }, "dependencies": { + "@wecom/crypto": "^1.0.0", "coa-error": "^1.5.2", "coa-helper": "^1.2.1", "coa-xml": "^1.1.0" diff --git a/src/base/WxWorkBin.ts b/src/base/WxWorkBin.ts index 095189a..071b538 100644 --- a/src/base/WxWorkBin.ts +++ b/src/base/WxWorkBin.ts @@ -1,7 +1,5 @@ import { CoaError } from 'coa-error' import { $, axios, Axios, _ } from 'coa-helper' -import { xml } from 'coa-xml' -import { createDecipheriv } from 'crypto' import { readFileSync } from 'fs' import { basename } from 'path' import { WxWorkStorage } from '../libs/WxWorkStorage' @@ -49,25 +47,4 @@ export class WxWorkBin { return { headers, data } } - - async decrypt(encryptedData: string, aesKey: string) { - const key = Buffer.from(aesKey, 'base64') - const iv = key.slice(0, 16) - let result = {} as any - - try { - const decipher = createDecipheriv('aes-256-cbc', key, iv) - // 设置自动 padding 为 true,删除填充补位 - decipher.setAutoPadding(true) - let decoded = decipher.update(encryptedData, 'base64', 'utf8') - try { - decoded += decipher.final('utf8') - } catch (e) {} - decoded = decoded.replace(/[\s\S]*([\s\S]*<\/xml>)[\s\S]*/, '$1') - result = await xml.decode(decoded) - } catch (e) { - console.error('微信解密失败', e) - } - return result - } } diff --git a/src/base/WxWorkServiceAuth.ts b/src/base/WxWorkServiceAuth.ts index 777dab4..c5ca398 100644 --- a/src/base/WxWorkServiceAuth.ts +++ b/src/base/WxWorkServiceAuth.ts @@ -1,5 +1,7 @@ +import { decrypt } from '@wecom/crypto' import { CoaError } from 'coa-error' import { _ } from 'coa-helper' +import { xml } from 'coa-xml' import { WxWork } from '../typings' import { WxWorkBin } from './WxWorkBin' import { WxWorkService } from './WxWorkService' @@ -20,9 +22,32 @@ export class WxWorkServiceAuth extends WxWorkService { } // 解密 - async decrypt(encrypted: string) { + decrypt(encrypted: string) { const aesKey = this.agent.aesKey ?? CoaError.message('WxWork.Missing', '缺少AesKey,无法解析数据') - return await this.bin.decrypt(encrypted, aesKey) + const { message } = decrypt(aesKey, encrypted) + return message + } + + // 解密XML + async decryptXml(encrypted: string) { + const data = this.decrypt(encrypted) + try { + return await xml.decode(data) + } catch (e) { + console.error('微信解密失败', e) + return undefined + } + } + + // 解密JSON + async decryptJson(encrypted: string) { + const data = this.decrypt(encrypted) + try { + return JSON.parse(data) + } catch (e) { + console.error('微信解密失败', e) + return undefined + } } // 获取Token diff --git a/yarn.lock b/yarn.lock index c7e8597..f680bb4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -169,6 +169,11 @@ "@typescript-eslint/types" "4.31.0" eslint-visitor-keys "^2.0.0" +"@wecom/crypto@^1.0.0": + version "1.0.0" + resolved "https://registry.yarnpkg.com/@wecom/crypto/-/crypto-1.0.0.tgz#68c84d41da9e591a50b756ea9236f29819360a8b" + integrity sha512-Rv+BX25s6V4yvpXoGgtishof1Kij2hfmM5VUVEkwRjCtqmI47t+xj48QEKulzJTMt7O0Y2uuZwshPxkIjb6/zg== + acorn-jsx@^5.3.1: version "5.3.2" resolved "https://registry.yarnpkg.com/acorn-jsx/-/acorn-jsx-5.3.2.tgz#7ed5bb55908b3b2f1bc55c6af1653bada7f07937"