1.安装包:npm i jsencrypt --save
2.封装函数
import { JSEncrypt } from 'jsencrypt';
export function setEncrypt(msg) {
const jsencrypt = new JSEncrypt();
jsencrypt.setPublicKey('公钥');
return jsencrypt.encrypt(msg);
}
export function decrypt(msg) {
const decrypt = new JSEncrypt();
decrypt.setPrivateKey('私钥');
const decryptMsg = decrypt.decrypt(msg);
return decryptMsg;
}
2.sm-crypto
1.安装包: npm i sm-crypto --save
2.封装函数
import { sm2 } from 'sm-crypto';
const keypair = sm2.generateKeyPairHex()
const publicKey = keypair.publicKey
const privateKey = keypair.privateKey
const PBLICKEY = '041a9f945535fb9be8f9ae11299a82baa99762ebdea672a902c85c986d70fd4b8dc841b062ff0cf964a930ae4a0ac3cedbaf1f46f983f60d3bb68f3f27a89a8b7c'
const PRIVATEKEY = '16e73c8c3646d7d724efb8fa56e2616da6a824ae81ad6c91026b2a05d1c2d0e1'
export function setEncrypt(msg) {
const pubKey = `${PBLICKEY}`;
const cipherMode = 1;
const encryptData = sm2.doEncrypt(msg, pubKey, cipherMode);
return encryptData;
}
export function decrypt(msg) {
const priKey = `${PRIVATEKEY}`;
const cipherMode = 1;
const decryptData = sm2.doDecrypt(msg, priKey, cipherMode);
return decryptData;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?