uniappRAS加密
1.安装wxmp-rsa
npm i wxmp-rsa -S
2.新建rsa.js文件
import WxmpRsa from 'wxmp-rsa'
//
//公钥和私钥换自己的
// 定义公钥
const publicKey = `
-----BEGIN PUBLIC KEY-----
MIGeMA0GCSqGSIb3DQEBAQUAA4GMADCBiAKBgFnWSUwsmGawhMJ30z6y5li2jcf1
m7rPMZcwZOS3To8bk3OBaMGhVEc1F8GtJBbc1rn/HCLNL9zrCy21EefJON8tRFcY
HnpseZSzh==
-----END PUBLIC KEY-----
`
// 定义私钥
const privateKey = `
-----BEGIN RSA PRIVATE KEY-----
MIICWgIBAAKBgFnWSUwsmGawhMJ30z6y5li2jcf1m7rPMZcwZOS3To8bk3OBaMGh
VEc1F8GtJBbc1rn/HCLNL9zrCy21EefJON8tRFcYHnpseZSzh+349lIhS+MFw9x4
JUddwSPDyxwha929cKzMuVoftu3CJ+kqDBVvxLk7iDBzUMqW3Kgehk2TAgMBAAEC
gYBRChPeyk/EOWtk/kbaOXafSofu0NcQYx4Km3M3kiP
lcNfTJ8CQQCVPcaRpu+mprRgHS6s76Z668NaFsjX04CUUa0kCrey+Nf/SJJ3BkRH
M7GllZWuI/RSXs/F5N38p5bfkn7QZqaNAkBy3dHJZW8DpgjdYOFnhAxwFK39BwGx
zHhWtv26kWbCcTKwsp+jtB4vunm3k+RmiN6aeGM35L6jt+kdJ0JYLmo7AkBJpRZb
wZj5D8Jqu3vQ8uGgPr9DsYKinkgQ6M0bv/4uXwWXf+Rmv7zpteSv5UTbjfp+uzKk
YO/6QWj+InhZto3xAkAOA0i702dLHm5elLWvht7UEYIDEW1+rYGdbthmJBvT9sZh
VKL954Y9hDzBWepjYsBiJnmIkgeladPnU5025/G/
-----END RSA PRIVATE KEY-----
`
//加密
function getRsaCode(str) {
const rsa = new WxmpRsa()
rsa.setPublicKey(publicKey);
const data = rsa.encryptLong(str)
return data
}
//解密
function getRsaDeCode(str) {
const rsa = new WxmpRsa()
rsa.setPrivateKey(privateKey);
const data = rsa.decryptLong(str)
return data
}
export default {
getRsaCode,
getRsaDeCode,
};
3.在所需要的文调用
//这是测试方法,具体根据需求更改
onLoad() {
let str = {name:'878998'}
//加密
let a=rsa.getRsaCode(JSON.stringify(this.username)) //注意要转为字符串
console.log("加密前",a)
// 密文
//解密
let b=rsa.getRsaDeCode(c)
console.log("解密后",b)
},
公钥密钥生成:http://www.metools.info/code/c81.html 可以用这个软件测试
公钥密钥已删除部分内容,仅为示例