encrypt.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * @Author: 15902849627 380091478@qq.com
  3. * @Date: 2024-06-12 15:37:21
  4. * @LastEditors: 15902849627 380091478@qq.com
  5. * @LastEditTime: 2024-06-12 15:59:21
  6. * @FilePath: \Platform-Web\src\lib\encrypt.js
  7. * @Description:
  8. *
  9. * Copyright (c) 2024 by ${git_name_email}, All Rights Reserved.
  10. */
  11. import CryptoJS from 'crypto-js';
  12. import CryptoSM from 'sm-crypto';
  13. function object2string(data) {
  14. if (typeof data === 'Object') {
  15. return JSON.stringify(data);
  16. }
  17. let str = JSON.stringify(data);
  18. if (str.startsWith("'") || str.startsWith('"')) {
  19. str = str.substring(1);
  20. }
  21. if (str.endsWith("'") || str.endsWith('"')) {
  22. str = str.substring(0, str.length - 1);
  23. }
  24. return str;
  25. }
  26. // ----------------------- AES 加密、解密 -----------------------
  27. const AES_KEY = 'cc81a41aee1a6078b6cbb3950f3e3c38';
  28. const AES = {
  29. encryptData: function (data) {
  30. // AES 加密 并转为 base64
  31. let utf8Data = CryptoJS.enc.Utf8.parse(object2string(data));
  32. const key = CryptoJS.enc.Utf8.parse(AES_KEY);
  33. const encrypted = CryptoJS.AES.encrypt(utf8Data, key, {
  34. mode: CryptoJS.mode.ECB,
  35. padding: CryptoJS.pad.Pkcs7,
  36. });
  37. return encrypted.toString();
  38. },
  39. decryptData: function (data) {
  40. // 第一步:Base64 解码
  41. let words = CryptoJS.enc.Base64.parse(data);
  42. // 第二步:AES 解密
  43. const key = CryptoJS.enc.Utf8.parse(AES_KEY);
  44. return CryptoJS.AES.decrypt({ ciphertext: words }, key, {
  45. mode: CryptoJS.mode.ECB,
  46. padding: CryptoJS.pad.Pkcs7,
  47. }).toString(CryptoJS.enc.Utf8);
  48. },
  49. };
  50. // ----------------------- 国密SM4算法 加密、解密 -----------------------
  51. const SM4_KEY = 'cc81a41aee1a6078b6cbb3950f3e3c38';
  52. const SM4 = {
  53. encryptData: function (data) {
  54. // 第一步:SM4 加密
  55. let encryptData = CryptoSM.sm4.encrypt(object2string(data), SM4_KEY);
  56. // 第二步: Base64 编码
  57. return CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(encryptData));
  58. },
  59. decryptData: function (data) {
  60. // 第一步:Base64 解码
  61. let words = CryptoJS.enc.Base64.parse(data);
  62. let decode64Str = CryptoJS.enc.Utf8.stringify(words);
  63. // 第二步:SM4 解密
  64. return CryptoSM.sm4.decrypt(decode64Str, SM4_KEY);
  65. },
  66. };
  67. // ----------------------- 对外暴露: 加密、解密 -----------------------
  68. // 默认使用SM4算法
  69. const EncryptObject = SM4;
  70. // const EncryptObject = AES;
  71. /**
  72. * 加密
  73. */
  74. export const encryptData = function (data) {
  75. return !data ? null : EncryptObject.encryptData(data);
  76. };
  77. /**
  78. * 解密
  79. */
  80. export const decryptData = function (data) {
  81. return !data ? null : EncryptObject.decryptData(data);
  82. };