zoukankan      html  css  js  c++  java
  • 使用MD5+SHA1混合进行加盐加密

    首先,我们来看一下原始的密码,这里我们简单点,使用了123456作为测试用的密码,但是真实生活中,最好不要把密码设为123456.

    string password = "123456";
    

    OK,那么现在我们来创建加密对象

    MD5 md5 = MD5.Create();//创建MD5加密对象
    SHA1 sha1 = SHA1.Create();//创建SHA1加密对象
    

    然后我们需要开始进行密码加密了

    byte[] data = Encoding.Default.GetBytes("D" + password + "R");//以D和R做盐,进行加盐加密,这样就算你的原始密码泄露,加密后的密码也无法被破解
    byte[] data_md5 = md5.ComputeHash(data);//先进行MD5加密
    byte[] data_md5_sha1 = sha1.ComputeHash(data_md5);//再进行SHA1二次加密
    

    这里我们使用了D和R作为"盐",分别放在字符串前面和后面,这样可以保证明文密码被破解了还是无法获得我们加密后的密码,更加保证了安全性.

    然后我们需要使用StringBuilder获得我们加密后的密码:

    StringBuilder builder = new StringBuilder();
    foreach (var item in data_md5_sha1)
    {
        builder.Append(item.ToString("x2"));//再把加密后的密码转换为16进制,防止暴力破解
    }
    
    string password_with_encrypted = builder.ToString();//得到加密后的新密码
    

    在这期间我们还把每个字符转换为了16进制,这是为了防止暴力破解.

    用完之后,我们最好清空加密对象

    md5.Clear();
    sha1.Clear();
    

    输出看结果:

    Console.WriteLine("The password with encrypted is :" + password_with_encrypted);
    The password with encrypted is :fa5c941309545bdb150bcd78db45395a0403edc4
    
    Everything has its time and that time must be watched. If you have any question, please contact email linqtosql@qq.com.
  • 相关阅读:
    求一个电子书制作的好方法
    解决windows xp系统,报iis提示访问人数过多错误
    DEVELOPER: ODP.NET Instant ODP.NET Deployment
    重新注册Oracle相关库
    ODP.NET调用存储需要使用事务
    ReportViewer在设计报告参数(SetParameter)时线程挂起(hang)
    js 时间
    验证码
    script标签属性用type还是language?
    iScroll框架的修改
  • 原文地址:https://www.cnblogs.com/Fill/p/7287175.html
Copyright © 2011-2022 走看看