CodeMgr.ts 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. const crypto = require("crypto");
  2. export default class CodeMgr {
  3. /**
  4. * 加密
  5. */
  6. static jiami(obj: any) {
  7. let keyBuf = this._base64_decode("65gkjU43HuMbTNhgp8pEMNhTTpLCfBgh"); //秘钥 必须24位
  8. let ivBuf = this._base64_decode("z0pA3Hxm7SQ"); // 偏移量 必须8位
  9. const cipher = crypto.createCipheriv("des-ede3-cbc", keyBuf, ivBuf);
  10. let encrypted = cipher.update(JSON.stringify(obj), "utf8", "base64");
  11. encrypted += cipher.final("base64");
  12. return encrypted;
  13. }
  14. /**
  15. * 解密
  16. */
  17. static jiemi(encrypted: string) {
  18. let keyBuf = this._base64_decode("65gkjU43HuMbTNhgp8pEMNhTTpLCfBgh"); //秘钥 必须24位
  19. let ivBuf = this._base64_decode("z0pA3Hxm7SQ"); // 偏移量 必须8位
  20. const decipher = crypto.createDecipheriv("des-ede3-cbc", keyBuf, ivBuf);
  21. let decrypted = decipher.update(encrypted, "base64", "utf8");
  22. decrypted += decipher.final("utf8");
  23. let json = JSON.parse(decrypted);
  24. return json;
  25. }
  26. //base 64 解码 输出buff 而不是字符串
  27. static _base64_decode(code: string): Buffer {
  28. var b = Buffer.from(code, "base64");
  29. return b;
  30. }
  31. }