使用Javascript实现一种简单的数字混淆功能
时间:2010-09-25 来源:lifans
上网的朋友有时候需要在网上发布一些信息,如求职、交友、二手交易、租房等。发布这些信息的时候肯定会涉及到个人信息,如QQ号、手机号,但是有些别有用心的人可能会利用这些信息进行诈骗,或对我们进行电话、短信骚扰。笔者在网上发现有些朋友在网上发布信息时会故意将手机号码不写成阿拉伯数字而是写成汉字的谐音。这是一种比较好的办法,使用这种方法可以防止被搜索引擎搜到,也能降低被不良之人利用的几率。好了,闲话少说,上代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>数字混淆器-混淆QQ、手机号...</title>
<style type="text/css">
.txtbox
{
border-top:0;
border-left:0;
border-right:0;
border-bottom:1px gray solid;
width: 300px;
}
.info
{
font-size:10pt;
color:red;
margin-left:10px;
}
</style>
<script type="text/javascript">
function Create(strSource)
{
var code = new Array();
var result = "";
code[0] = new Array("0","1","2","3","4","5","6","7","8","9");
code[1] = new Array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
code[2] = new Array("(0)","①","②","③","④","⑤","⑥","⑦","⑧","⑨");
code[3] = new Array("O","一","二","三","四","五","六","七","八","九");
code[4] = new Array("灵","易","尔","伞","斯","吴","留","期","巴","久");
for(var i = 0; i < strSource.length; i++)
{
var row = GetRandom();
var col = eval(strSource.substr(i, 1));
if(strSource.substr(i, 1) == " ")
{
result += strSource.substr(i, 1);
}
else
{
result += "" + code[row][col];
}
}
return result;
}
function GetRandom()
{
var result = ~~(Math.random() * 5);
return result;
}
function GetCode()
{
var source = document.getElementById("txtCode").value;
var str = new String();
document.getElementById("txtResult").value = Create(source);
}
</script>
</head>
<body>
<form id="form1">
号码:<input class="txtbox" type="input" id="txtCode" /><span class="info">注:此输入项只能为数字或空格</span><br />
结果:<input class="txtbox" style="color:Blue;" type="input" id="txtResult" /><br />
<input type="button" id="btnOk" value="生成" onclick="GetCode();" />
<input type="reset" id="btnClear" value="清空" />
</form>
</body>
</html>
我们来看一下效果吧,输入手机号13923456789,单击“生成”,混淆后的号码就直接显示出来了(图1)。哈哈,这下连自己都认不出来了吧,需要自己辨认才能看明白。
图1
再单击一下“生成”就会生成另外一种混淆结果(图2),因为每个数字使用那种表示方式都是随机的,所以每次都会生成新的混淆结果。
图二
好了,本文就到此吧。由于笔者水平有限,以上代码代码可能还存在不足之处,欢迎各位朋友讨论。