Skip to content

Commit

Permalink
fix: 加解密工具bug修复
Browse files Browse the repository at this point in the history
1. AES解密方法,当加密内容为假值时,抛出异常
2. 微信消息解密方法,当推送xml没有Encrypt节点时,抛出异常
  • Loading branch information
jay committed Aug 31, 2024
1 parent 12d1d1d commit efaabf2
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/utils/message-crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,16 @@ export class MessageCrypto {
* 对给定的密文进行AES解密
* @param {Buffer} aesKey
* @param {Buffer} iv
* @param {String} str
* @param {String} encryptMessage
* @returns
*/
public static decrypt (aesKey: Buffer, iv: Buffer, str: string) {
public static decrypt (aesKey: Buffer, iv: Buffer, encryptMessage: string) {
if (!encryptMessage) {
throw new Error('no encrypt message');
}
const aesCipher = crypto.createDecipheriv('aes-256-cbc', aesKey, iv);
aesCipher.setAutoPadding(false);
const decipheredBuff = MessageCrypto.PKCS7Decoder(Buffer.concat([aesCipher.update(str, 'base64'), aesCipher.final()]));
const decipheredBuff = MessageCrypto.PKCS7Decoder(Buffer.concat([aesCipher.update(encryptMessage, 'base64'), aesCipher.final()]));
const data = decipheredBuff.slice(16);
const msgLen = data.slice(0, 4).readUInt32BE(0);
return data.slice(4, msgLen + 4).toString();
Expand Down Expand Up @@ -158,6 +161,9 @@ export class MessageCrypto {
const parser = new XMLParser();
const xml = parser.parse(encryptXml).xml;
const encryptMessage = xml.Encrypt;
if (!encryptMessage) {
throw new Error('No Encrypt');
}
if (signature !== MessageCrypto.sha1(token || '', timestamp, nonce, encryptMessage)) {
throw new Error('signature incorrect');
}
Expand Down

0 comments on commit efaabf2

Please sign in to comment.