const crypto = require("crypto"); export default class CodeMgr { /** * 加密 */ static jiami(obj: any) { let keyBuf = this._base64_decode("65gkjU43HuMbTNhgp8pEMNhTTpLCfBgh"); //秘钥 必须24位 let ivBuf = this._base64_decode("z0pA3Hxm7SQ"); // 偏移量 必须8位 const cipher = crypto.createCipheriv("des-ede3-cbc", keyBuf, ivBuf); let encrypted = cipher.update(JSON.stringify(obj), "utf8", "base64"); encrypted += cipher.final("base64"); return encrypted; } /** * 解密 */ static jiemi(encrypted: string) { let keyBuf = this._base64_decode("65gkjU43HuMbTNhgp8pEMNhTTpLCfBgh"); //秘钥 必须24位 let ivBuf = this._base64_decode("z0pA3Hxm7SQ"); // 偏移量 必须8位 const decipher = crypto.createDecipheriv("des-ede3-cbc", keyBuf, ivBuf); let decrypted = decipher.update(encrypted, "base64", "utf8"); decrypted += decipher.final("utf8"); let json = JSON.parse(decrypted); return json; } //base 64 解码 输出buff 而不是字符串 static _base64_decode(code: string): Buffer { var b = Buffer.from(code, "base64"); return b; } }