直接加密跟解密是引入jsencrypt.js直接调用方法就可以;分段加密的话是需要修改jsencrypt.js添加encryptLong()和decryptLong()方法;
附代码:
加密:
var encrypt = new JSEncrypt();
encrypt.setPublicKey(this.pubKey);
encrypt.enctypt("要加密的内容")
解密:
var decrypt = new JSEncrypt();
decrypt.setPrivateKey(privateKey);
uncrypted = decrypt.decrypt("要解密内容");
分段加密:
JSEncrypt.prototype.encryptLong = function(string) {
var k = this.getKey();
var maxLength = (((k.n.bitLength()+7)>>3)-11);
try {
var lt = "";
var ct = "";
if (string.length > maxLength) {
lt = string.match(/.{1,245}/g);
lt.forEach(function(entry) {
var t1 = k.encrypt(entry);
ct += t1 ;
});
return hex2b64(ct);
}
var t = k.encrypt(string);
var y = hex2b64(t);
return y;
} catch (ex) {
return false;
}
};
分段解密:
JSEncrypt.prototype.decryptLong = function(string) {
var k = this.getKey();
var maxLength = ((k.n.bitLength()+7)>>3);
try {
var string = b64tohex(string);
var ct = "";
if (string.length > maxLength) {
var lt = string.match(/.{1,512}/g);
lt.forEach(function(entry) {
var t1 = k.decrypt(entry);
ct += t1;
});
return ct;
}
var y = k.decrypt(b64tohex(string));
return y;
} catch (ex) {
return false;
}
};
如有不对,望留言修正 谢谢。
网友评论