javascript RSA公钥加密
程序员文章站
2022-07-09 13:48:09
...
PC端:
引入js
<script type="text/javascript" src="https://passport.cnblogs.com/scripts/jsencrypt.min.js"></script>
使用:
const PUBLIC_KEY =
'-----BEGIN PUBLIC KEY-----\
MIIBIjAvpvRqhkiG9w0BAQEFAAOCAQ8AMII\
BCgKCAQEAz0LFPPBZYkV/d9mkM1Zmuv3lwgy\
-----END PUBLIC KEY-----';
var encrypt = new JSEncrypt();
encrypt.setPublicKey(PUBLIC_KEY);
var encryptData = encrypt.encrypt("data"); //data必须转成字符串
微信小程序:
js Github链接:https://github.com/UFO0001/WX_RSA
用法:
var data= this.data;
var encrypt_rsa = new RSA.RSAKey();
encrypt_rsa = RSA.KEYUTIL.getKey(PUBLIC_KEY);
encStr = encrypt_rsa.encrypt(data)
encStr = RSA.hex2b64(encStr);
console.log("加密结果:" + encStr)