js 字符串加密解密

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">

    <title>网页加密及解密</title>
    <meta name="author" content="xiongzaiqiren" />
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <meta name="generator" content="ClassBao team coding in July 10, 2017" />

</head>

<body>
    <p><textarea id="text1" name="textfield" cols="50" rows="5">钱庄王员外这个人怎么样?</textarea></p>
    <input type="button" name="Button1" value="加密" onClick="text1.value = MySign.Encrypt(text1.value);">
    <input type="button" name="Button2" value="解密" onClick="text1.value = MySign.UnEncrypt(text1.value);">

    <script language="JavaScript">
        var MySign = {
            //加密/解密次数
            num: 0,
            //加密
            Encrypt: function (Text) {
                this.num = this.num + 1;
                output = new String;
                alterText = new Array();
                varCost = new Array();
                TextSize = Text.length;
                for (i = 0; i < TextSize; i++) {
                    idea = Math.round(Math.random() * 111) + 77;
                    alterText[i] = Text.charCodeAt(i) + idea;
                    varCost[i] = idea;
                }
                console.log(alterText, varCost);
                for (i = 0; i < TextSize; i++) {
                    output += String.fromCharCode(alterText[i], varCost[i]);
                }
                //text1.value = output;
                return output;
            },

            //解密
            UnEncrypt: function (Text) {
                if (this.num > 0) {
                    this.num = this.num - 1;
                    output = new String;
                    alterText1 = new Array();
                    varCost1 = new Array();
                    TextSize = Text.length;
                    for (i = 0; i < TextSize; i++) {
                        alterText[i] = Text.charCodeAt(i);
                        varCost[i] = Text.charCodeAt(i + 1);
                    }
                    for (i = 0; i < TextSize; i = i + 2) {
                        output += String.fromCharCode(alterText[i] - varCost[i]);
                    }
                    //text1.value = output;
                    return output;
                }
            }
        };


        //测试代码
        var testString = "光头强,还不去砍树?";
        console.log(testString);

        var sign = MySign.Encrypt(testString); //凑妣o忕ァ[還¬什³呯´硠S桲aチb
        var sign2 = MySign.UnEncrypt(sign); //光头强,还不去砍树?

        console.log(sign);
        console.log(sign2);

    </script>

</body>
</html>

 

转载于:https://www.cnblogs.com/hss-blog/p/10109565.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值