• MD5加密封装


    加密32位

     /// <summary>
            /// MD5字符串加密
            /// </summary>
            /// <param name="txt"></param>
            /// <returns>加密后字符串</returns>
            public static string GenerateMD5(string txt)
            {
                using (MD5 mi = MD5.Create())
                {
                    byte[] buffer = Encoding.Default.GetBytes(txt);
                    //开始加密
                    byte[] newBuffer = mi.ComputeHash(buffer);
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < newBuffer.Length; i++)
                    {
                        sb.Append(newBuffer[i].ToString("x2"));
                    }
                    return sb.ToString();
                }
            }

    1.固定返回固定长度字符串(16位或者32位)

    /// <summary>
    /// 用MD5加密字符串,可选择生成16位或者32位的加密字符串
    /// </summary>
    /// <param name="password">待加密的字符串</param>
    /// <param name="bit">位数,一般取值16 或 32</param>
    /// <returns>返回的加密后的字符串</returns>
    public string MD5Encrypt(string password, int bit)
    {
    MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
    byte[] hashedDataBytes;
    hashedDataBytes = md5Hasher.ComputeHash(Encoding.GetEncoding("gb2312").GetBytes(password));
    StringBuilder tmp = new StringBuilder();
    foreach (byte i in hashedDataBytes)
    {
    tmp.Append(i.ToString("x2"));
    }
    if (bit == 16)
    return tmp.ToString().A(8, 16);
    else
    if (bit == 32) return tmp.ToString();//默认情况
    else return string.Empty;
    }
     2. 加密字符串
    复制代码
    /// <summary>
    /// 用MD5加密字符串
    /// </summary>
    /// <param name="password">待加密的字符串</param>
    /// <returns></returns>
    public string MD5Encrypt(string password)
    {
    MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
    byte[] hashedDataBytes;
    hashedDataBytes = md5Hasher.ComputeHash(Encoding.GetEncoding("gb2312").GetBytes(password));
    StringBuilder tmp = new StringBuilder();
    foreach (byte i in hashedDataBytes)
    {
    tmp.Append(i.ToString("x2"));
    }
    return tmp.ToString();
    }
  • 相关阅读:
    构造函数和析构函数
    关联[2]
    关联模型[1]
    auth 权限控制
    多语言设置
    文件上传
    验证码
    图像处理
    Session 与 Cookie
    控制器[3]
  • 原文地址:https://www.cnblogs.com/yuanshuo/p/11528319.html
Copyright © 2020-2023  润新知