Skip to content

Commit

Permalink
feat(yanwen): add aesDecrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
wangjue666 committed Aug 8, 2024
1 parent 62462be commit af1b8f6
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions packages/yanwen/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable @typescript-eslint/restrict-plus-operands */
import crypto from 'crypto';
import {
postJSONRequest,
Recordable,
Expand Down Expand Up @@ -60,4 +61,31 @@ export class YanWen {
}
return res as any as T;
}

/**
* @description 解密燕文更新账单信息请求的数据
*/
aesDecrypt(key: string, encryptedBase64: string) {
const initialDigest = crypto.createHash('sha1').update(key).digest();
const sKey = crypto
.createHash('sha1')
.update(initialDigest)
.digest()
.slice(0, 16);

// The IV is an empty string in this case, which is not used in ECB mode
const iv = '';

// Decode the base64 string
const encryptedBuffer = Buffer.from(encryptedBase64, 'base64');

// Decrypt the data
const decipher = crypto.createDecipheriv('aes-128-ecb', sKey, iv);
decipher.setAutoPadding(true);
// @ts-expect-error
const decrypted = decipher.update(encryptedBuffer, 'binary', 'utf8');
const decryptedJsonStr = decrypted + decipher.final('utf8');

return JSON.parse(decryptedJsonStr);
}
}

0 comments on commit af1b8f6

Please sign in to comment.