//公钥私钥可以在网上在线生成,公钥加密私钥解密
// 定义公钥
var public_key = `-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCm7rVusH+6r5KzgzVKfvpXXuQ7
W8TAIGWApuxKLuKiPM8vwEfjZn0SDRGGzHEJfIyQQvFI+L172Q5Fg/OJ7AbH21uq
02NSj6nmbsJjQlbO3ioC1DDOdK5RaO/3DFEr0rZHhIj0ePBi382zSweDV+3MkthJ
gemv6ApHynEsTefogwIDAQAB
-----END PUBLIC KEY-----`
// 定义私钥
var private_key = `-----BEGIN PRIVATE KEY-----
MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKbutW6wf7qvkrOD
NUp++lde5DtbxMAgZYCm7Eou4qI8zy/AR+NmfRINEYbMcQl8jJBC8Uj4vXvZDkWD
84nsBsfbW6rTY1KPqeZuwmNCVs7eKgLUMM50rlFo7/cMUSvStkeEiPR48GLfzbNL
B4NX7cyS2EmB6a/oCkfKcSxN5+iDAgMBAAECgYA7Ug6lVBE/zQ6Lvi+c1nytuanP
MjA7ugd37VmAjyK4VnWl2WVyeMQvOvekeFge7+SfizplHQdw0FRS/a34sChEIEdk
W7cuo1TitKFJX1uQSOX0nK+CB5syJbO45N3YJnJyKY46WlACJVDbOv0u1ZAJaKli
J0i02E+nKJ+kdEEQAQJBANOz0B8EzSpyVhescF+EAuDIpEF9Na502FuZkFqQ8oGt
gXaN7VNVOy1VHaoZD7H5gXyvcRX/yss9hr8wL8aQEIMCQQDJ3LhoSRUJi9/dKLk/
CeTtHr1Jahr0fRmsRAxlock2tk14Cocwv5FL97oA/+51CjZY7dzYFy6QHV3V1VlO
EUgBAkEAv8Pg4UgzJKD/VWBxRkRJax60k1H9H3NeTNM0zKUB1u+5FxAHC3ZoLW8t
eNhMvFWbsrslP9098I5iEatvi3kSSwJABbw1qRDafijxNVFg4f+f93S2IA3ZsjST
tCdCyuhr1Rgc31o9OQ9YS3ZAXKLo3X5qyOAJNEKEWifbcKXdIbCwAQJAJqkRGUne
jBkA8NyBVY5QoEhK9JUX79zvVAamnaDo7sbc6jtaS4CjbABxTiPgwu3Yicxbu9E4
1+YOu5WfMowiHA==
-----END PRIVATE KEY-----`
var encrypt = new JSEncrypt();
encrypt.setPublicKey(public_key);
encrypt.setPrivateKey(private_key);
//待加密数据
var data={
num:123456,
str:"abcdefg",
list:[1,2,3],
obj:{
a:1,
b:2
}
}
//加密后的数据
var encryptData=encrypt.encrypt(JSON.stringify(data));
//解密后的数据
var decryptData=encrypt.decrypt(encryptData);
console.log(data);
console.log(encryptData);
console.log(JSON.parse(decryptData));