• 全部
  • 问答

满分求教如何用javascript编写加密算法

zgh007 2004-01-06 12:11:17
满分求教如何用javascript编写加密算法
...全文
46 点赞 收藏 6
写回复
6 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
hotenM 2004-09-09
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin

function encrypt(str, 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 == 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;
alert('bg');
while(prand.length > 10) {
alert(prand);
alert(prand.substring(10, prand.length))
alert(parseInt(prand.substring(10, prand.length)))
prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();

}
prand = (mult * prand + incr) % modu;
alert(prand);
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 enc_str;
}
// End -->
</script>
<form name="box"><center>
<table cellpadding=0 cellspacing=0 border=0><tr><td colspan=3>
<textarea cols=40 rows=5 wrap=virtual name=ipt>Welcome to 51js.com</textarea>
</td></tr>
<tr height=50><td valign="top">
<input type="button" onclick="document.box.opt.value= encrypt(document.box.ipt.value, document.box.pwd.value);" value="加密">
</td><td align="center" valign="center">
<input type="text" name="pwd" value="password">
</td><td align="right" valign="bottom">
<input type="button" onclick="document.box.ipt.value= decrypt(document.box.opt.value, document.box.pwd.value);" value="解密">
</td></tr>
<tr><td colspan=3>
<textarea cols=40 rows=5 wrap=virtual name=opt></textarea>
</td></tr></table>
</center>
</form>
回复
banditind 2004-04-09
关注,我查了好久,没有看到有函数可以调用,msdn中有:
[JScript]
public static function Encrypt(
strData : String
) : String;

我用
document.form1.encryptpass.value = Encrypt("12345");
但看到encryptpass=
encryptpass仍然是空的,

请指点一二
回复
mariner 2004-01-06
关注……
回复
swich 2004-01-06
用md5,目前最好的加密
回复
fason 2004-01-06
md5

http://search.csdn.net/expert/topic/3/304/2002/6/20/817127.htm
回复
仙人掌 2004-01-06
给个例子

function encode(datastr) {
var tempstr;
var tchar;
var newdata = "";

for (var i = 0; i < datastr.length; i++)
{
tchar = 65535 - datastr.charCodeAt(i);
tchar = tchar.toString();

while(tchar.length < 5)
{
tchar = "0" + tchar;
}

newdata = newdata + tchar;
}

return newdata;
}
回复
发帖
JavaScript
创建于2007-09-28

8.5w+

社区成员

Web 开发 JavaScript
申请成为版主
帖子事件
创建了帖子
2004-01-06 12:11
社区公告
暂无公告