最近閑來無事給自己寫了家庭財(cái)務(wù)收支管理系統(tǒng),也就包含支出管理,收入管理和一些統(tǒng)計(jì)功能。
先說登錄模塊,因?yàn)樯婕癎ET和POST請(qǐng)求,這些東西都是能被監(jiān)控和抓取的所以就考慮這使用RSA加密解密方式傳輸用戶名和密碼參數(shù),頁面JS如下:
/*需要引入三個(gè)JS文件,BigInt.js、RSA.js和Barrett.js,用到cookie則需要引入jquery.cookie.js文件*///與后臺(tái)交互獲取公鑰function getPublicKey() { var pubKey = ''; if ($.cookie('publicKey') == null) { $.ajax({ url: "/Account/GetRsaPublicKey", type: "get", contentType: "application/x-www-form-urlencoded; charset=utf-8", async: false, data: {}, dataType: "json", success: function (data) { if (data.Code == 0) { pubKey = data.RsaPublicKey + "," + data.Key; $.cookie('publicKey', pubKey, { expires: 1 / 1440 }); } else { Config.Method.JudgeCode(data, 1); } } }); } else { pubKey = $.cookie('publicKey'); } return pubKey;}//公鑰加密用戶密碼Pwd為RSA加密后參數(shù)function rsaEncrypt(pwd) { var publicKey = getPublicKey(); setMaxDigits(129); var rsaKey = new RSAKeyPair(publicKey.split(",")[0], "", publicKey.split(",")[1]); var pwdRtn = encryptedString(rsaKey, pwd); return pwdRtn + "," + publicKey.split(",")[2];}//POST登錄請(qǐng)求,參數(shù)<script type="text/javascript"> $(function () { $('#btnSubmit').live('click', function () { var uName = $('#u').val(); var pwd = $('#p').val(); if (uName == '') { alert('用戶名不能為空'); return; } if (pwd == '') { alert('用戶密碼不能為空'); return; } var enPwd = rsaEncrypt(pwd); $.ajax({ type: "POST", url: "/Account/UserLogin", data: { 'UserName': uName, 'Pwd': enPwd.split(",")[0], 'Key': enPwd.split(",")[1], 'RUrl': $('#hiddenUrl').val() }, contentType: "application/x-www-form-urlencoded; charset=utf-8", async: false, dataType: "json", success: function (data) { if (data.result == true) { window.location.href = data.url; return false; } else { $('#msg').text(data.message); } }, error: function (XMLHttpRequest, textStatus, errorThrown) { $('#msg').text(XMLHttpRequest.status + '||' + XMLHttpRequest.readyState + '||' + textStatus); } }); }); })</script>前臺(tái)加密完成后就需要后臺(tái)做解密處理,解密完成后需要使用MD5加密現(xiàn)有密碼與數(shù)據(jù)庫(kù)中用戶密碼進(jìn)行比較驗(yàn)證,如果驗(yàn)證通過則需要寫入cookie以便下次用戶能自 動(dòng)登錄,由于cookie中我不希望用戶名和密碼都明碼存儲(chǔ),我這里用到了AES加密的方式,自定義一個(gè)32位的加密密鑰對(duì)cookie進(jìn)行加密解密處理,后臺(tái)c#代碼如 下:
新聞熱點(diǎn)
疑難解答
圖片精選