/*
* 字符串加密
* 用javascript对字符串进行加密,应用于参数传递等。
* 默认加密密钥为kb1234,使用者可自定义修改。
* 注意加密密钥应和解密密钥相同。
* 算法来自于互联网
* 使用方法:
* 加密
* var code = kbt.encrypt('我爱北京天安门');
* alert(code);
* 解密
* var str = kbt.decrypt(code);
* alert(str);
*/
var kbt = (function () {
var that = {};
function encrypt(str, pwd) {
if(str == '') {
return '';
}
str = encodeURIComponent(str);
if(!pwd || pwd == '') {
pwd = 'kb1234';
}
pwd = encodeURIComponent(pwd);
if(pwd == '' || pwd.length <= 0) {
return '';
}
var prand = '';
for(var i = 0, len = pwd.length; i < len; i += 1) {
prand += pwd.charCodeAt(i).toString();
}
var sPos = Math.floor(prand.length / 5);
var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos * 2) + prand.charAt(sPos * 3) +
prand.charAt(sPos * 4) + prand.charAt(sPos * 5));
var incr = Math.ceil(pwd.length / 2);
var modu = Math.pow(2, 31) - 1;
if(mult < 2) {
return '';
}
var salt = Math.round(Math.random() * 1000000000) % 100000000;
prand += salt;
while(prand.length > 10) {
prand = (parseInt(prand.substring(0, 10)) +
parseInt(prand.substring(10, prand.length))).toString();
}
prand = (mult * prand + incr) % modu;
var encChr = '';
var encStr = '';
for(var i = 0, len = str.length; i < len; i += 1) {
encChr = parseInt(str.charCodeAt(i) ^ Math.floor((prand / modu) * 255));
if(encChr < 16) {
encStr += '0' + encChr.toString(16);
}else{
encStr += encChr.toString(16);
}
prand = (mult * prand + incr) % modu;
}
salt = salt.toString(16);
while(salt.length < 8) {
salt = "0" + salt;
}
encStr += salt;
return encStr;
}
function decrypt(str, pwd) {
if(str == ''){
return '';
}
if(!pwd || pwd == ''){
pwd = 'kb1234';
}
pwd = encodeURIComponent(pwd);
if(str == undefined || str.length < 8) {
return '';
}
if(pwd == undefined || pwd.length <= 0) {
return '';
}
var prand = '';
for(var i = 0, len = pwd.length; i < len; i += 1) {
prand += pwd.charCodeAt(i).toString();
}
var sPos = Math.floor(prand.length / 5);
var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos * 2) + prand.charAt(sPos * 3) +
prand.charAt(sPos * 4) + prand.charAt(sPos * 5));
var incr = Math.round(pwd.length / 2);
var modu = Math.pow(2, 31) - 1;
var salt = parseInt(str.substring(str.length - 8, str.length), 16);
str = str.substring(0, str.length - 8);
prand += salt;
while(prand.length > 10) {
prand = (parseInt(prand.substring(0, 10)) +
parseInt(prand.substring(10, prand.length))).toString();
}
prand = (mult * prand + incr) % modu;
var encChr = '';
var encStr = '';
for(var i = 0, len = str.length; i < len; i += 2) {
encChr = parseInt(parseInt(str.substring(i, i + 2), 16) ^ Math.floor((prand / modu) * 255));
encStr += String.fromCharCode(encChr);
prand = (mult * prand + incr) % modu;
}
return decodeURIComponent(encStr);
}
that.encrypt = encrypt;
that.decrypt = decrypt;
return that;
})();
js加密字符串
于 2023-03-07 16:58:51 首次发布
该文章提供了一个使用JavaScript实现的字符串加密和解密的函数示例。默认密钥为kb1234,用户可以自定义。加密过程基于特定算法,包括编码、密钥处理和异或操作,解密过程则相反。适用于参数传递等场景。
摘要由CSDN通过智能技术生成