用户登录 3 /// RSA加密密码 [Route()] 7 [HttpPost] 8 [Anonymous] 9 public LoginResult Login([FromBody] LoginModel loginModel) 10 { 11 var privateKey = CacheDataManager.GetPrivateKey(loginModel.key); 12 if (!string.IsNullOrEmpty(privateKey)) 13 { CacheDataManager.RemoveKey(privateKey); (string.IsNullOrEmpty(loginModel.phoneNumber)) 18 { UserDisplayException(); 20 } (string.IsNullOrEmpty(loginModel.password)) 23 { UserDisplayException(); 25 } password = Security.DecryptRSA(loginModel.password, privateKey); 28 loginModel.password = password; result = accountInfoService.User_Login(loginModel.phoneNumber, loginModel.password, loginModel.userType); token = CreateToken(result.UUID, loginModel.userType.ToString()); LoginResult() 36 { 37 Code = 0, 38 UserPrefect = result.UserPrefect, 39 Token = token, 40 IMName = result.IMName, 41 IMPassword = result.IMPassword, 42 LetterIntentCount = result.LetterIntentCount 43 }; 44 } { Exception(); 48 } 49 }
注:
1.我们需要客户端回传key值,以确认该用户使用公钥对应密钥
2.对密文进行RSA解密
那么我们再来看看前端界面应该怎么做
1.我们需要三个文件:Barrett.js BigInt.js RSA.js
下载地址:
2.在引用jquery后添加对三个文件的引用
></script> ></script> ></script> ></script> ></script>
3.写一个获取公钥的js方法到common.js文件(尽量将这个方法的调用放在用户不经意之间,在用户触发登录事件之前执行一次,避免等到用户点击登录的时候再调用造成停顿 )
getPublicKey=function () {
3
if(getCookie("publicKey")==null){
4
$.ajax({
,
,
,
8
async: false,
9
data: {},
,
11
success: function (data) {
12
if (data.Code == 0) {
+ data.Key;
setCookie("publicKey", publicKey,8);// 此处存储时间应该小于后台缓存时间
return publicKey;
14
} else {
15
Config.Method.JudgeCode(data, 1);
16
}
17
}
18
});
}else{
return getCookie("publicKey");
}
19
}
4.写一个通用的js加密方法到common.js
rsaEncrypt: function (pwd) { 3 var publicKey=getPublicKey(); 4 setMaxDigits(129); )[)[1]); 6 var pwdRtn = encryptedString(rsaKey, pwd); 7 return pwdRtn+","+publicKey.split(",")[2]; 8 },
5.来看看我们在登录按钮的js方法中具体调用:
).val(); ).val(); 3 if (!userName.length) { ); 5 return; 6 } 7 if (!pwd.length) { ); 9 return; 10 } 11 if (!Config.Datas.RegPhone.test(userName)) { ); 13 return; 14 } 15 if (!Config.Datas.PasswordVerification.test(pwd)) { ); 17 return; 18 } pwd1 = Config.Method.rsaEncrypt(pwd); : userName, : pwd1.split(",")[0], : , : publicKey.split()[1] }, function (data) { 23 publicKey = ""; 24 if (data.Code == 0) { 25 Config.Method.SetCookies(data.Token, userName, data.UserPrefect, data.LetterIntentCount); ).html(data.LetterIntentCount); 27 _login_registEvent(); 28 Config.Method.InitLoginInfo(); Config.Method.JudgeCode(data, 0); 34 } 35 });
OK,至此我们就简单的实现了用JS进行RSA加密,c#解密的基本功能。
对安全性这一块一直没什么研究,这次的项目又不要求...
一直想学习学习大神们对于整个安全性做的操作,在博客园找了很久也没找到特别详细通俗易懂的....
在此,也恳请各位大神能分享一下自己这方面的经验,感激不尽。
原创文章,代码都是从自己项目里贴出来的。转载请注明出处哦,亲~~~