`

javascript简单加密解密函数

阅读更多

javascript简单加密解密函数

<title>JS加密函数</title>
<SCRIPT LANGUAGE="JavaScript">
var str_in;
var str_out = "";
var num_in;
var num_out = "";
var e = "Enter Text!";
// 加密
function str_to_num(form) {
    num_out = "";
    if (form.input.value == ""){
		alert(e);
	} else {
        str_in = escape(form.input.value);
        for (i = 0; i < str_in.length; i++) {
            num_out += str_in.charCodeAt(i) - 23;
        }
        form.output.value = num_out;
        form.input.value = "";
    }
}
// 解密
function num_to_str(form) {
    str_out = "";
    if (form.output.value == "") alert(e)
    else {
        num_out = form.output.value;
        for (i = 0; i < num_out.length; i += 2) {
            num_in = parseInt(num_out.substr(i, [2])) + 23;
            num_in = unescape('%' + num_in.toString(16));
            str_out += num_in;
        }
        form.input.value = unescape(str_out);
        form.output.value = "";
    }
}
</script>
<body>
<center>
<form name=encryptform>
    <table>
      <tr> 
        <td align=center> 
          <input name=input type=text size=38 value="www.codefans.net">
        </td>
        <td align=center> 
          <input type=button value="<--" onClick="javascript:num_to_str(this.form)">
          <br>
          <input type=button value="-->" onClick="javascript:str_to_num(this.form)">
        </td>
        <td align=center> 
          <input name=output type=text size=38>
        </td>
      </tr>
    </table>
</form>
</center>

 将页面的中文编码

var passWord = encodeURI(encodeURI("中国");

 

然后可以再服务端使用

URLDecoder.decode("", "utf-8");

 解码使用

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics