| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- /*
- * @Author: 15902849627 380091478@qq.com
- * @Date: 2024-06-12 15:37:21
- * @LastEditors: 15902849627 380091478@qq.com
- * @LastEditTime: 2024-06-12 15:59:21
- * @FilePath: \Platform-Web\src\lib\encrypt.js
- * @Description:
- *
- * Copyright (c) 2024 by ${git_name_email}, All Rights Reserved.
- */
- import CryptoJS from 'crypto-js';
- import CryptoSM from 'sm-crypto';
- function object2string(data) {
- if (typeof data === 'Object') {
- return JSON.stringify(data);
- }
- let str = JSON.stringify(data);
- if (str.startsWith("'") || str.startsWith('"')) {
- str = str.substring(1);
- }
- if (str.endsWith("'") || str.endsWith('"')) {
- str = str.substring(0, str.length - 1);
- }
- return str;
- }
- // ----------------------- AES 加密、解密 -----------------------
- const AES_KEY = 'cc81a41aee1a6078b6cbb3950f3e3c38';
- const AES = {
- encryptData: function (data) {
- // AES 加密 并转为 base64
- let utf8Data = CryptoJS.enc.Utf8.parse(object2string(data));
- const key = CryptoJS.enc.Utf8.parse(AES_KEY);
- const encrypted = CryptoJS.AES.encrypt(utf8Data, key, {
- mode: CryptoJS.mode.ECB,
- padding: CryptoJS.pad.Pkcs7,
- });
- return encrypted.toString();
- },
- decryptData: function (data) {
- // 第一步:Base64 解码
- let words = CryptoJS.enc.Base64.parse(data);
- // 第二步:AES 解密
- const key = CryptoJS.enc.Utf8.parse(AES_KEY);
- return CryptoJS.AES.decrypt({ ciphertext: words }, key, {
- mode: CryptoJS.mode.ECB,
- padding: CryptoJS.pad.Pkcs7,
- }).toString(CryptoJS.enc.Utf8);
- },
- };
- // ----------------------- 国密SM4算法 加密、解密 -----------------------
- const SM4_KEY = 'cc81a41aee1a6078b6cbb3950f3e3c38';
- const SM4 = {
- encryptData: function (data) {
- // 第一步:SM4 加密
- let encryptData = CryptoSM.sm4.encrypt(object2string(data), SM4_KEY);
- // 第二步: Base64 编码
- return CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(encryptData));
- },
- decryptData: function (data) {
- // 第一步:Base64 解码
- let words = CryptoJS.enc.Base64.parse(data);
- let decode64Str = CryptoJS.enc.Utf8.stringify(words);
- // 第二步:SM4 解密
- return CryptoSM.sm4.decrypt(decode64Str, SM4_KEY);
- },
- };
- // ----------------------- 对外暴露: 加密、解密 -----------------------
- // 默认使用SM4算法
- const EncryptObject = SM4;
- // const EncryptObject = AES;
- /**
- * 加密
- */
- export const encryptData = function (data) {
- return !data ? null : EncryptObject.encryptData(data);
- };
- /**
- * 解密
- */
- export const decryptData = function (data) {
- return !data ? null : EncryptObject.decryptData(data);
- };
|