关于加密.NET

 

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Security.Cryptography;

/// <summary>
///PasswordUtil 的摘要说明
/// </summary>
public class PasswordUtil
{
 public PasswordUtil()
 {
  //
  //TODO: 在此处添加构造函数逻辑
  //
 }
    #region field & constructor

    //private static readonly Log _log = new Log(typeof(PasswordUtil));

    private const int saltLength = 4;

    #endregion

    /// <summary>
    /// 对比用户明文密码是否和加密后密码一致
    /// </summary>
    /// <param name="dbPassword">数据库中单向加密后的密码</param>
    /// <param name="userPassword">用户明文密码</param>
    /// <returns></returns>
    public static bool ComparePasswords(string dbPassword, string userPassword)
    {
        byte[] dbPwd = Convert.FromBase64String(dbPassword);

        byte[] hashedPwd = HashString(userPassword);

        if (dbPwd.Length == 0 || hashedPwd.Length == 0 || dbPwd.Length != hashedPwd.Length + saltLength)
        {
            return false;
        }

        byte[] saltValue = new byte[saltLength];
        // int saltOffset = dbPwd.Length - hashedPwd.Length;
        int saltOffset = hashedPwd.Length;
        for (int i = 0; i < saltLength; i++)
            saltValue[i] = dbPwd[saltOffset + i];

        byte[] saltedPassword = CreateSaltedPassword(saltValue, hashedPwd);

        // compare the values
        return CompareByteArray(dbPwd, saltedPassword);


    }

    /// <summary>
    /// 创建用户的数据库密码
    /// </summary>
    /// <param name="password"></param>
    /// <returns></returns>
    public static string CreateDbPassword(string userPassword)
    {
        byte[] unsaltedPassword = HashString(userPassword);

        //Create a salt value
        byte[] saltValue = new byte[saltLength];
        RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
        rng.GetBytes(saltValue);

        byte[] saltedPassword = CreateSaltedPassword(saltValue, unsaltedPassword);

        return Convert.ToBase64String(saltedPassword);

    }

    #region 私有函数
    /// <summary>
    /// 将一个字符串哈希化
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    private static byte[] HashString(string str)
    {
        byte[] pwd = System.Text.Encoding.UTF8.GetBytes(str);

        SHA1 sha1 = SHA1.Create();
        byte[] saltedPassword = sha1.ComputeHash(pwd);
        return saltedPassword;
    }
    private static bool CompareByteArray(byte[] array1, byte[] array2)
    {
        if (array1.Length != array2.Length)
            return false;
        for (int i = 0; i < array1.Length; i++)
        {
            if (array1[i] != array2[i])
                return false;
        }
        return true;
    }
    // create a salted password given the salt value
    private static byte[] CreateSaltedPassword(byte[] saltValue, byte[] unsaltedPassword)
    {
        // add the salt to the hash
        byte[] rawSalted = new byte[unsaltedPassword.Length + saltValue.Length];
        unsaltedPassword.CopyTo(rawSalted, 0);
        saltValue.CopyTo(rawSalted, unsaltedPassword.Length);

        //Create the salted hash   
        SHA1 sha1 = SHA1.Create();
        byte[] saltedPassword = sha1.ComputeHash(rawSalted);

        // add the salt value to the salted hash
        byte[] dbPassword = new byte[saltedPassword.Length + saltValue.Length];
        saltedPassword.CopyTo(dbPassword, 0);
        saltValue.CopyTo(dbPassword, saltedPassword.Length);

        return dbPassword;
    }
    #endregion
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值