js加密,解密

来源:互联网 发布:湖北快三数据 编辑:程序博客网 时间:2024/04/30 20:58
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="BaiYu.WebUI.Login" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>检察机关案件质量督查平台</title>
    <script type="text/javascript">
        if (window != window.top) {
            window.open(window.location.href, "_top");
        }
    </script>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <style type="text/css">
<!--
body {
margin:0;
padding:0;
background:url(images/login_body_bg.jpg) repeat 0 0;overflow-y:hidden ;
}
table,td,tr{ border:none; padding:0; margin:0;}
.warper{ width:1003px; height:679px; margin:0 auto; border:none; background:url(images/login_bg.jpg) no-repeat 0 0;}
.login_input{ font:14px/28px "宋体"; border:none; width:211px; height:34px; background:url(images/login_input_bg.jpg) no-repeat 0 0; padding-left:10px;}
.inpass{font:14px/28px "宋体"; padding-right:13px;}
.in_bnt {background:url(images/login_input_bg.jpg) no-repeat right bottom; width:133px; height:51px; border:none; cursor:hand;}
-->
</style>
<script type="text/javascript">
    /***************加密,解密方法*****************/
    //加密
    function Encrypt(str, pwd) {
        if (str == "") return "";
        str = escape(str);
        if (!pwd || pwd == "") { var pwd = "123456"; }
        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 = "123456"; }
        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 -->


    window.onload = function () {
        document.getElementById("BrowserWidth").value = window.screen.width;
        document.getElementById("BrowserHeight").value = window.screen.height;
        var UserName = document.getElementById("Username").vlaue;
        document.getElementById('Username').focus();
        
        if (isPostBack == "False") {
            //取最后一个cookie赋值密码、用户框
            GetLastUser();
        }
    }


    //获取最后一个用户cookie(包括用户名和密码)
    function GetLastUser() {
        var id = "49BAC005-7D5B-4231-8CEA-16939BEACD67"; //GUID标识符
        var usr = GetCookie(id);
        if (usr != null) {
            document.getElementById('Username').value = usr;
            document.getElementById('Password').focus();
        } else {
            document.getElementById('Username').value = "";
        }
        GetPwdAndChk();
    }
    //加密字符串
    var mima = "123456";
    //加密后的字符串
    var DePass = "";
    //输入的真实密码
    var TruePass = "";
    //点击登录时触发客户端事件
    function SetPwdAndChk() {
        //取用户名
        var usr = document.getElementById('Username').value;
        //将最后一个用户信息写入到Cookie
        SetLastUser(usr);
        //如果记住密码选项被选中
        if (document.getElementById('chkRememberPwd').checked == true) {
            //取密码值
            var pwd = document.getElementById('Password').value;
            TruePass = pwd;
            pwd = Encrypt(mima, pwd);
            DePass = pwd;
            var expdate = new Date();
            expdate.setTime(expdate.getTime() + 14 * (24 * 60 * 60 * 1000));
            //将用户名和密码写入到Cookie
            SetCookie(usr, pwd, expdate);
        } else {
            //如果没有选中记住密码,则立即过期
            ResetCookie();
        }
    }
    //设置最后一个用户cookie
    function SetLastUser(usr) {
        var id = "49BAC005-7D5B-4231-8CEA-16939BEACD67";
        var expdate = new Date();
        //当前时间加上两周的时间
        expdate.setTime(expdate.getTime() + 14 * (24 * 60 * 60 * 1000));
        SetCookie(id, usr, expdate);
    }
    //用户名失去焦点时调用该方法
    function GetPwdAndChk() {
        var usr = document.getElementById('Username').value;
        //var pwd = GetCookie(usr);
        var pwd = Decrypt(GetCookie(usr), TruePass);
        //alert(TruePass);
        if (pwd != null) {
            document.getElementById('chkRememberPwd').checked = true;
            document.getElementById('Password').value = pwd;
        } else {
            document.getElementById('chkRememberPwd').checked = false;
            document.getElementById('Password').value = "";
        }
    }
    //取Cookie的值
    function GetCookie(name) {
        var arg = name + "=";
        var alen = arg.length;
        var clen = document.cookie.length;
        var i = 0;
        while (i < clen) {
            var j = i + alen;
            //alert(j);
            if (document.cookie.substring(i, j) == arg) return getCookieVal(j);
            i = document.cookie.indexOf(" ", i) + 1;
            if (i == 0) break;
        }
        return null;
    }
    var isPostBack = "<%= IsPostBack %>";
    function getCookieVal(offset) {
        var endstr = document.cookie.indexOf(";", offset);
        if (endstr == -1) endstr = document.cookie.length;
        return unescape(document.cookie.substring(offset, endstr));
    }
    //写入到Cookie
    function SetCookie(name, value, expires) {
        var argv = SetCookie.arguments;
        //本例中length = 3
        var argc = SetCookie.arguments.length;
        var expires = (argc > 2) ? argv[2] : null;
        var path = (argc > 3) ? argv[3] : null;
        var domain = (argc > 4) ? argv[4] : null;
        var secure = (argc > 5) ? argv[5] : false;
        document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : "");
    }
    //重置cookie
    function ResetCookie() {
        var usr = document.getElementById('Username').value;
        var expdate = new Date();
        SetCookie(usr, null, expdate);
    }
</script>
</head>
<body scroll="no">
    <form action="?action=login&<%=url %>" method="post" id="formLogin">
    <table class="warper">
        <tr>
            <td valign="top">
                <table width="1003" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                        <td width="425" height="315">
                            &nbsp;
                        </td>
                        <td width="221">
                            &nbsp;
                        </td>
                        <td width="357">
                            &nbsp;
                        </td>
                    </tr>
                    <tr>
                        <td height="181">
                            &nbsp;
                        </td>
                        <td valign="top">
                            <form id="form1" name="form1" method="post" action="">
                            <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                <tr>
                                    <td>
                                        <label>
                                            <input class="login_input" id="Username" name="Username" type="text" maxlength="10"  onblur="GetPwdAndChk()" 
                                                 value="" />
                                        </label>
                                    </td>
                                </tr>
                                <tr>
                                    <td height="5">
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <label>
                                            <input class="login_input" id="Password" name="Password" type="password" runat="server" />
                                        </label>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right" class="inpass">
                                        <label>
                                            <input name="chkRememberPwd" id="chkRememberPwd" type="checkbox" checked="checked" runat="server" />
                                            记住密码
                                        </label>
                                    </td>
                                </tr>
                                <tr>
                                    <td height="5">
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right">
                                        <label>
                                            <input type="submit" name="Submit" value="" class="in_bnt" onclick="SetPwdAndChk()" />
                                        </label>
                                        <input type="hidden" name="BrowserWidth" id="BrowserWidth" runat="server" />
                                        <input type="hidden" name="BrowserHeight" id="BrowserHeight" runat="server" />
                                    </td>
                                </tr>
                            </table>
                            </form>
                        </td>
                        <td>
                            &nbsp;
                        </td>
                    </tr>
                    <tr>
                        <td>
                            &nbsp;
                        </td>
                        <td>
                            &nbsp;
                        </td>
                        <td>
                            &nbsp;
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>
原创粉丝点击