javascript将url中的参数加密解密代码
作者:hebedich 时间:2024-04-23 09:33:09
今天在做一个老项目时,遇到一个需求,在javascript将url中的参数加密解密,从网上找发现了这段有用的代码:
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function Encrypt(str, pwd) {
if(str=="")return "";
str = escape(str);
if(!pwd || pwd==""){ var pwd="1234"; }
pwd = escape(pwd);
if(pwd == null || pwd.length <= 0) {
alert("Please enter a password with which to encrypt the message.");
return null;
}
var prand = "";
for(var I=0; I<pwd.length; I++) {
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) {
alert("Algorithm cannot find a suitable hash. Please choose a different password. /nPossible considerations are to choose a more complex or longer password.");
return null;
}
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 enc_chr = "";
var enc_str = "";
for(var I=0; I<str.length; I++) {
enc_chr = parseInt(str.charCodeAt(I) ^ Math.floor((prand / modu) * 255));
if(enc_chr < 16) {
enc_str += "0" + enc_chr.toString(16);
}else
enc_str += enc_chr.toString(16);
prand = (mult * prand + incr) % modu;
}
salt = salt.toString(16);
while(salt.length < 8)salt = "0" + salt;
enc_str += salt;
return enc_str;
}
function Decrypt(str, pwd) {
if(str=="")return "";
if(!pwd || pwd==""){ var pwd="1234"; }
pwd = escape(pwd);
if(str == null || str.length < 8) {
alert("A salt value could not be extracted from the encrypted message because it's length is too short. The message cannot be decrypted.");
return;
}
if(pwd == null || pwd.length <= 0) {
alert("Please enter a password with which to decrypt the message.");
return;
}
var prand = "";
for(var I=0; I<pwd.length; I++) {
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 enc_chr = "";
var enc_str = "";
for(var I=0; I<str.length; I+=2) {
enc_chr = parseInt(parseInt(str.substring(I, I+2), 16) ^ Math.floor((prand / modu) * 255));
enc_str += String.fromCharCode(enc_chr);
prand = (mult * prand + incr) % modu;
}
return unescape(enc_str);
}
// End -->
</script>
以后碰到加密解密问题,直接将上述代码写成一个js文件,就搞定。省事了。。。。
标签:javascript,加密解密
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Transactional replication(事务复制)详解之如何跳过一个事务
2024-01-19 15:41:16
![](https://img.aspxhome.com/file/2023/6/94446_0s.png)
PyQt5每天必学之QSplitter实现窗口分隔
2021-12-26 16:33:48
解决ajax+php中文乱码的方法详解
2024-06-05 09:49:08
![](https://img.aspxhome.com/file/2023/9/122449_0s.gif)
用Python写漏洞验证脚本的代码
2023-09-04 05:38:36
![](https://img.aspxhome.com/file/2023/3/80663_0s.jpg)
python实现简易五子棋游戏(控制台版)
2022-01-14 14:38:30
![](https://img.aspxhome.com/file/2023/2/70202_0s.jpg)
go语言代码生成器code generator使用示例介绍
2024-05-21 10:19:29
![](https://img.aspxhome.com/file/2023/0/125070_0s.png)
python用opencv批量截取图像指定区域的方法
2021-10-14 09:07:33
PyQt5使用mimeData实现拖拽事件教程示例解析下
2021-02-15 11:09:03
![](https://img.aspxhome.com/file/2023/2/97642_0s.gif)
MySQL查询优化之explain的深入解析
2024-01-17 02:29:02
Django项目如何给数据库添加约束
2023-08-10 14:49:39
![](https://img.aspxhome.com/file/2023/4/62014_0s.jpg)
SQL 提权 常用命令
2024-01-23 03:21:45
python中的Pyperclip模块功能详解
2021-10-25 05:28:22
pandas 使用均值填充缺失值列的小技巧分享
2023-01-21 02:29:25
antd项目实现彩蛋效果的详细代码
2023-09-14 12:51:20
![](https://img.aspxhome.com/file/2023/1/56321_0s.jpg)
Python实现自动登录百度空间的方法
2023-11-11 09:11:23
Python超细致探究面向对象
2023-05-28 17:02:56
pandas检查和填充缺失值的N种方法总结
2021-03-27 03:25:21
![](https://img.aspxhome.com/file/2023/6/80806_0s.png)
pycharm无法安装cv2模块问题及解决方案
2023-02-14 21:20:49
![](https://img.aspxhome.com/file/2023/9/133819_0s.png)
使用一条INSERT语句完成多表插入
2010-03-18 11:08:00
python 实现一个贴吧图片爬虫的示例
2023-03-01 08:43:28
![](https://img.aspxhome.com/file/2023/6/115656_0s.png)