让京东的密码问题从此不再重现(NET篇)
来源:互联网 发布:怎么安装oracle数据库 编辑:程序博客网 时间:2024/04/27 16:11
2012年12月,以CSDN为首的密码泄露门第一次出现在互联网中,这样的爆发让我们这些基层程序猿们不可以理解,csdn居然没有把密码加密。哦买噶!!!转眼2016年12月,时隔4年京东电商巨头又一次遭遇了这样的悲催行为。这样的问题,就是密码简单设置导致的。
开篇结束进入正题,我是一枚NET开发者,如果通过密码安全来保护用户的隐私?这里有一个很好的解决方案,首先在项目中做一个枚举如下图:
让平台来设置不同的用户不同的加密方式(Clear可以去掉不用)。
其次,我们在写一个加密服务EncryptionService
public class EncryptionService { /// <summary> /// 加密 /// </summary> /// <param name="password"></param> /// <param name="saltkey"></param> /// <param name="passwordFormat"></param> /// <returns></returns> public string CreatePasswordHash(string password, string saltkey, string passwordFormat = "SHA1") { if (String.IsNullOrEmpty(passwordFormat)) passwordFormat = "SHA1"; string saltAndPassword = String.Concat(password, saltkey); //return FormsAuthentication.HashPasswordForStoringInConfigFile(saltAndPassword, passwordFormat); var algorithm = HashAlgorithm.Create(passwordFormat); if (algorithm == null) throw new ArgumentException("无法识别的哈希名称"); var hashByteArray = algorithm.ComputeHash(Encoding.UTF8.GetBytes(saltAndPassword)); return BitConverter.ToString(hashByteArray).Replace("-", ""); } /// <summary> /// 加密 /// </summary> /// <param name="plainText"></param> /// <param name="encryptionPrivateKey"></param> /// <returns></returns> public string EncryptText(string plainText, string encryptionPrivateKey = "") { if (string.IsNullOrEmpty(plainText)) return plainText; return MD5Encrypt(plainText, new UTF8Encoding()); } public string MD5Encrypt(string input, Encoding encode) { MD5 md5 = new MD5CryptoServiceProvider(); byte[] t = md5.ComputeHash(encode.GetBytes(input)); StringBuilder sb = new StringBuilder(32); for (int i = 0; i < t.Length; i++) sb.Append(t[i].ToString("x").PadLeft(2, '0')); return sb.ToString(); } }
好了,做好了这两个方法,我们来设计数据库的用户表。
因为是个教程,我没有刻意去设计很好的CodeFirst或是映射。我用的ef。大家凑合看
上图数据库设计
不用太多解释,PasswordFormatId是针对不同用户设计的不同加密方式,即便全部泄露了,解密也要很复杂。
最后在来看看控制器
public class CustomerController : Controller { #region Login // GET: Customer public ActionResult Index(string tip = "") { var model = new CustomerIndexModel(); ViewBag.Tip = tip; return View(model); } [HttpPost] public ActionResult Login(CustomerIndexModel model) { var result = new CustomerService().Login(model.LoginName); if(result == null) return RedirectToAction("Index", new { tip = "该账户不存在" }); bool Verification = false; switch ((PasswordFormat)result.PasswordFormatId) { case PasswordFormat.Clear: Verification = model.LoginPassword == result.Password; break; case PasswordFormat.Encrypted: Verification = new EncryptionService().EncryptText(model.LoginPassword, result.PasswordSalt) == result.Password; break; case PasswordFormat.Hashed: Verification = new EncryptionService().CreatePasswordHash(model.LoginPassword, result.PasswordSalt) == result.Password; break; default: Verification = new EncryptionService().CreatePasswordHash(model.LoginPassword, result.PasswordSalt) == result.Password; break; } if (!Verification) return RedirectToAction("Index", new { tip = "密码错误" }); else return RedirectToAction("Index", new { tip = "登录成功" }); } #endregion #region Register public ActionResult Register() { var model = new CustomerRegisterModel(); return View(model); } [HttpPost] public ActionResult Register(CustomerRegisterModel model) { //判定是否录入验证通过 if (ModelState.IsValid) { var entity = new Models.Customer { Active = true, Deleted = false, Email = model.Email, LoginName = model.LoginName, Password = model.LoginPassword, Mobile = model.Mobile, //假设加密方式为 PasswordFormatId = (int)PasswordFormat.Hashed }; new CustomerService().InsertCusotmer(entity); return RedirectToAction("Index", new { tip ="注册成功" }); } return View(); } #endregion } 资源下载地址:http://download.csdn.net/detail/hb12417058/9711008
这个不用过多解释。
大家如果有什么疑问请加群 68848430 咨询。
我会把资源上传上来供大家学习
1 0
- 让京东的密码问题从此不再重现(NET篇)
- 从此不再有"时间问题"
- 传智播客,从此,不再低调!
- 修改Hosts极速下载Android的ADT和SDK,从此不再烦恼无法下载的问题了
- 人邮出版社出的书 -- 从此不再光顾
- 从此不怕MYSQL字符集的问题
- 如何定位"无法重现“的问题
- 从此不再迷信Google--mvnforum中lucene搜索中文的解决办法
- 80后的“娶妻标准”——从此不再担心80后
- const char * 的终结贴(看完无需其他任何文章,从此不再蛋疼)
- const char * 的终结贴(看完无需其他任何文章,从此不再蛋疼)
- 当年第一次学习java多线程写的例子,从此多线程不再陌生
- const char* ptr 到底是什么意思【转载的,从此不再蛋疼】
- 从此不再更新博客,新的博客地址我会在以后公布
- Eclipse导出SVN项目的时候,不再提示输入用户名,密码问题
- 重现TortoiseSVN客户端的用户名密码验证框(收藏)
- 重现TortoiseSVN客户端的用户名密码验证框--转载
- 重现TortoiseSVN客户端的用户名密码验证框
- 心蓝12306订票助手-自己动手抢火车票
- Android中的引用类型(强引用,弱引用,软引用,虚引用)
- [kuangbin带你飞]专题四 最短路练习 -F
- NodeJS简要教程
- 高德地图——搜索
- 让京东的密码问题从此不再重现(NET篇)
- 响应式页面制作总结
- mac电脑如何抢火车票
- yii2图片上传
- spring 常用注解
- 深入理解 GRE tunnel
- linux下分割catalina.out文件,按天生成文件
- Uiautomator使用方法的简单介绍
- windows使用git时出现:warning: LF will be replaced by CRLF