下载sm4util
pnpm i gm-crypto
新建index.ts文件
import { SM4 } from "gm-crypto";
const sm4Config = {
key: "",
iv: "",
};
const encryptSM4 = (text) => {
return SM4.encrypt(text, sm4Config.key, {
iv: sm4Config.iv,
mode: SM4.constants.CBC,
inputEncoding: "utf8",
outputEncoding: "base64",
});
};
const decryptSM4 = (text) => {
return SM4.decrypt(text, sm4Config.key, {
iv: sm4Config.iv,
mode: SM4.constants.CBC,
inputEncoding: "base64",
outputEncoding: "utf8",
});
};
export { encryptSM4, decryptSM4 };
在使用的地方直接引入
import { encryptSM4 } from '@/utils'
const password = encryptSM4 (password)