Skip to content

Commit

Permalink
Refactor decrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
adaex authored Sep 18, 2021
1 parent 2e1376c commit ae47d5f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 25 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
23 changes: 0 additions & 23 deletions src/base/WxWorkBin.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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]*(<xml>[\s\S]*<\/xml>)[\s\S]*/, '$1')
result = await xml.decode(decoded)
} catch (e) {
console.error('微信解密失败', e)
}
return result
}
}
29 changes: 27 additions & 2 deletions src/base/WxWorkServiceAuth.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit ae47d5f

Please sign in to comment.