jsencrypt.js 下載地址
這里是正確加密的代碼
<--- 公鑰 --->
var _respubkey = `
-----BEGIN PUBLIC KEY-----
Hanoangoaidho124902704170237403095y01740923409347012709jiasdfijabidjfbibi......
-----END PUBLIC KEY-----
`;
function setRsa(text) {
var encrypt= new JSEncrypt();
encrypt.setPublicKey(_respubkey);
var resultTxt = encrypt.encrypt(text);
return encodeURIComponent(resultTxt);
}
問(wèn)題:報(bào)錯(cuò)“密文數(shù)據(jù)已損壞”
剛開(kāi)始直接return encrypt.encrypt(text)
,會(huì)因?yàn)樯a(chǎn)的編碼有特殊符號(hào)報(bào)錯(cuò)寡键,用encodeURIComponent(resultTxt)
編碼一下,防止http請(qǐng)求的時(shí)候,丟失特殊符號(hào)。后端需要做decodeURIComponent
解碼。