jsencrypt加密解密
Posted ur home
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jsencrypt加密解密相关的知识,希望对你有一定的参考价值。
安装
yarn add jsencrypt
方法封装
import JSEncrypt from 'jsencrypt/bin/jsencrypt.min.js'
// 密钥对生成 http://web.chacuo.net/netrsakeypair
const publicKey = `MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCtUGD2zoexHOWefrIybP39a6wa
1SaC8PrTyw2UvRAEUxPVlf1th8hxc7LZygleJc246X2gT5SaSCqc2r9vJGfaLXEs
4iJBrNWBkX/0W7p9UlMcluaOUIghQ9XDu5VtXqLhx2TXeLWFtO/1q92kTuavQO8e
tbalPOGA3LscDn/A+QIDAQAB`
const privateKey = `MIICxjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQI+yMuBcMym2cCAggA
MBQGCCqGSIb3DQMHBAg0GkRhca+0hgSCAoAXnrgTNyNna4GPaI7ovBGVg3e89Vxx
LgRsYl2tAYFleFZUZiBz4C4c9DwoNzhyKBPA2ZcQp0MWxxFG9jc0LdhPsNgmBu95
4WGYYm+Fq31sTTE+JCcRoBHAIqfAYLZwoDYm+gJ2mOnzqQTRCP5yde5mPpcFHS8n
ZD03kB0Hu/qMUyigFxzG5qqX3Kd2cNwrS2cecMVTxlRyvjGGeCaQKBq8IygSSOSw
2TleW3c2rqIz91p4+Ei1DdNe+SKFMdeOjXpCf0G60dVkBtkhs1AgOkQb0UANAsU9
aKgJ1+mz/X6JEUduvGQc8n7XuTDHnEKTtaE3QQ2NCxqHynI1dQiPJK3cvy85UhlE
08Cu/+01k2Wjvad3K2+IOnamvw/bPkNVTpBnpTzR/Agap/S6pYUkGZnL15AVXxn7
LICVRnBkROcIFf37wwyYgNOcOBj7CoEMJqm22tQBlwA25/CdF9q/E+UpoTW2mMDj
+3zs3swqanVVDMBEMugXO7wnvp2ju2ZosnwyAmppjC2vgb4TLbNBZL3eU5bnBXR3
7+BwSOVwoAHkI8gNohXRJKzzh/Hjd0+iOtqPSqzu8rGtRfdrSuwYxv9jVZo3yG5h
iCXHgAlAGwVdWxGQsIuihn9eSR/AhBCkJF/TGuy7wy8H6c0mbcourNFFw4D8UGZj
W+jImnV9DEHKu6xRQQoER4gb8WJJVYnAqXTbPkznmKi1/6NjJCfDUHqi3vqK0Z+F
CinFEv9S5TjTMJNMgDpdIzeYwEhDCqAEabILptJSh8yoYxrjKx+ncVcqcMXreD4K
iu/9ilmxEow/iKj03jgqep3se66EO5IcCkcpKUCKmpo5Z+2210VO7kIw`
// 加密
export const encrypt = (txt: string) => {
const encryptor = new JSEncrypt()
encryptor.setPublicKey(publicKey) // 设置公钥
return encryptor.encrypt(txt) // 对数据进行加密
}
// 解密
export const decrypt = (txt: string) => {
const encryptor = new JSEncrypt()
encryptor.setPrivateKey(privateKey) // 设置私钥
return encryptor.decrypt(txt) // 对数据进行解密
}
以上是关于jsencrypt加密解密的主要内容,如果未能解决你的问题,请参考以下文章