码迷,mamicode.com
首页 > 其他好文 > 详细

散列哈希加密

时间:2019-08-17 13:04:57      阅读:50      评论:0      收藏:0      [点我收藏+]

标签:ace   summary   build   hmac   lower   大小写   service   com   sys   

  /// <summary>
  /// 散列哈希加密(MD5 SHA HMACSHA)
  /// </summary>
  public static class HashAlgorithmEncrypt
  {
    /// <summary>
    ///返回值类型
    /// </summary>
    public enum UpperLower
    {
      /// <summary>
      /// 大写
      /// </summary>
      Upper,
      /// <summary>
      /// 小写
      /// </summary>
      Lower
    }

    /// <summary>
    /// 位数
    /// </summary>
    public enum Bit
    {
      /// <summary>
      /// 16位
      /// </summary>
      bit16,
      /// <summary>
      /// 32位
      /// </summary>
      bit32
    }

    #region MD5

    /// <summary>
    /// MD5加密
    /// </summary>
    /// <param name="data">要加密的明文</param>
    /// <param name="dataEncoding">加载要加密的明文的编码方式</param>
    /// <param name="bit">MD5加密16位或32位</param>
    /// <param name="upperLower">密文值返回大小写</param>
    /// <returns></returns>
    public static string MD5Encrypt(string data, Encoding dataEncoding, Bit bit, UpperLower upperLower)
    {
      using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
      {
        byte[] dataBytes = md5.ComputeHash(dataEncoding.GetBytes(data));
        string encryptStr = (bit == Bit.bit16) ? BitConverter.ToString(dataBytes, 4, 8) : BitConverter.ToString(dataBytes);
        encryptStr = encryptStr.Replace("-", "");
        return upperLower == UpperLower.Upper ? encryptStr.ToUpper() : encryptStr.ToLower();
      }
    }

    #endregion

    #region SHA

    /// <summary>
    /// SHA1
    /// </summary>
    /// <param name="data">要加密的明文</param>
    /// <param name="dataEncoding">加载要加密的明文的编码方式</param>
    /// <param name="upperLower">密文值返回大小写</param>
    /// <returns></returns>
    public static string SHA1Encrypt(string data, Encoding dataEncoding, UpperLower upperLower)
    {
      StringBuilder sb = new StringBuilder();
      using (SHA1Managed sha1 = new SHA1Managed())
      {
        byte[] dataBytes = dataEncoding.GetBytes(data);
        byte[] encryptBytes = sha1.ComputeHash(dataBytes);
        for (int i = 0; i < encryptBytes.Length; i++)
        {
          sb.Append(upperLower == UpperLower.Upper ? encryptBytes[i].ToString("X2") : encryptBytes[i].ToString("x2"));
        }
        return sb.ToString();
      }
    }

    /// <summary>
    /// SHA256
    /// </summary>
    /// <param name="data">要加密的明文</param>
    /// <param name="dataEncoding">加载要加密的明文的编码方式</param>
    /// <param name="upperLower">密文值返回大小写</param>
    /// <returns></returns>
    public static string SHA256Encrypt(string data, Encoding dataEncoding, UpperLower upperLower)
    {
      StringBuilder sb = new StringBuilder();
      using (SHA256Managed sha256 = new SHA256Managed())
      {
        byte[] dataBytes = dataEncoding.GetBytes(data);
        byte[] encryptBytes = sha256.ComputeHash(dataBytes);
        for (int i = 0; i < encryptBytes.Length; i++)
        {
          sb.Append(upperLower == UpperLower.Upper ? encryptBytes[i].ToString("X2") : encryptBytes[i].ToString("x2"));
        }
        return sb.ToString();
      }
    }

    /// <summary>
    /// SHA384
    /// </summary>
    /// <param name="data">要加密的明文</param>
    /// <param name="dataEncoding">加载要加密的明文的编码方式</param>
    /// <param name="upperLower">密文值返回大小写</param>
    /// <returns></returns>
    public static string SHA384Encrypt(string data, Encoding dataEncoding, UpperLower upperLower)
    {
      StringBuilder sb = new StringBuilder();
      using (SHA384Managed sha384 = new SHA384Managed())
      {
        byte[] dataBytes = dataEncoding.GetBytes(data);
        byte[] encryptBytes = sha384.ComputeHash(dataBytes);
        for (int i = 0; i < encryptBytes.Length; i++)
        {
          sb.Append(upperLower == UpperLower.Upper ? encryptBytes[i].ToString("X2") : encryptBytes[i].ToString("x2"));
        }
        return sb.ToString();
      }
    }

    /// <summary>
    /// SHA512
    /// </summary>
    /// <param name="data">要加密的明文</param>
    /// <param name="dataEncoding">加载要加密的明文的编码方式</param>
    /// <param name="upperLower">密文值返回大小写</param>
    /// <returns></returns>
    public static string SHA512Encrypt(string data, Encoding dataEncoding, UpperLower upperLower)
    {
      StringBuilder sb = new StringBuilder();
      using (SHA512Managed sha512 = new SHA512Managed())
      {
        byte[] dataBytes = dataEncoding.GetBytes(data);
        byte[] encryptBytes = sha512.ComputeHash(dataBytes);
        for (int i = 0; i < encryptBytes.Length; i++)
        {
          sb.Append(upperLower == UpperLower.Upper ? encryptBytes[i].ToString("X2") : encryptBytes[i].ToString("x2"));
        }
        return sb.ToString();
      }
    }

    #endregion

    #region HMACSHA

    /// <summary>
    /// HMACMD5
    /// </summary>
    /// <param name="data">要加密的明文</param>
    /// <param name="key">秘钥</param>
    /// <param name="dataEncoding">加载要加密的明文的编码方式</param>
    /// <param name="upperLower">密文值返回大小写</param>
    /// <returns></returns>
    public static string HMACMD5Encrypt(string data, string key, Encoding dataEncoding, UpperLower upperLower)
    {
      StringBuilder sb = new StringBuilder();
      using (HMACMD5 hmacmd5 = new HMACMD5())
      {
        hmacmd5.Key = System.Text.Encoding.UTF8.GetBytes(key);
        byte[] dataBytes = dataEncoding.GetBytes(data);
        byte[] encryptBytes = hmacmd5.ComputeHash(dataBytes);
        for (int i = 0; i < encryptBytes.Length; i++)
        {
          sb.Append(upperLower == UpperLower.Upper ? encryptBytes[i].ToString("X2") : encryptBytes[i].ToString("x2"));
        }
        return sb.ToString();
      }
    }

    /// <summary>
    /// HMACSHA1
    /// </summary>
    /// <param name="data">要加密的明文</param>
    /// <param name="key">秘钥</param>
    /// <param name="dataEncoding">加载要加密的明文的编码方式</param>
    /// <param name="upperLower">密文值返回大小写</param>
    /// <returns></returns>
    public static string HMACSHA1Encrypt(string data, string key, Encoding dataEncoding, UpperLower upperLower)
    {
      StringBuilder sb = new StringBuilder();
      using (HMACSHA1 hmacsha1 = new HMACSHA1())
      {
        hmacsha1.Key = System.Text.Encoding.UTF8.GetBytes(key);
        byte[] dataBytes = dataEncoding.GetBytes(data);
        byte[] encryptBytes = hmacsha1.ComputeHash(dataBytes);
        for (int i = 0; i < encryptBytes.Length; i++)
        {
          sb.Append(upperLower == UpperLower.Upper ? encryptBytes[i].ToString("X2") : encryptBytes[i].ToString("x2"));
        }
        return sb.ToString();
      }
    }

    /// <summary>
    /// HMACSHA256
    /// </summary>
    /// <param name="data">要加密的明文</param>
    /// <param name="key">秘钥</param>
    /// <param name="dataEncoding">加载要加密的明文的编码方式</param>
    /// <param name="upperLower">密文值返回大小写</param>
    /// <returns></returns>
    public static string HMACSHA256Encrypt(string data, string key, Encoding dataEncoding, UpperLower upperLower)
    {
      StringBuilder sb = new StringBuilder();
      using (HMACSHA256 hmacsha256 = new HMACSHA256())
      {
        hmacsha256.Key = System.Text.Encoding.UTF8.GetBytes(key);
        byte[] dataBytes = dataEncoding.GetBytes(data);
        byte[] encryptBytes = hmacsha256.ComputeHash(dataBytes);
        for (int i = 0; i < encryptBytes.Length; i++)
        {
          sb.Append(upperLower == UpperLower.Upper ? encryptBytes[i].ToString("X2") : encryptBytes[i].ToString("x2"));
        }
        return sb.ToString();
      }
    }

    /// <summary>
    /// HMACSHA384(结果有问题)
    /// </summary>
    /// <param name="data">要加密的明文</param>
    /// <param name="key">秘钥</param>
    /// <param name="dataEncoding">加载要加密的明文的编码方式</param>
    /// <param name="upperLower">密文值返回大小写</param>
    /// <returns></returns>
    public static string HMACSHA384Encrypt(string data, string key, Encoding dataEncoding, UpperLower upperLower)
    {
      StringBuilder sb = new StringBuilder();
      using (HMACSHA384 hmacsha384 = new HMACSHA384())
      {
        hmacsha384.Key = System.Text.Encoding.UTF8.GetBytes(key);
        byte[] dataBytes = dataEncoding.GetBytes(data);
        byte[] encryptBytes = hmacsha384.ComputeHash(dataBytes);
        for (int i = 0; i < encryptBytes.Length; i++)
        {
          sb.Append(upperLower == UpperLower.Upper ? encryptBytes[i].ToString("X2") : encryptBytes[i].ToString("x2"));
        }
        return sb.ToString();
      }
    }

    /// <summary>
    /// HMACSHA512
    /// </summary>
    /// <param name="data">要加密的明文</param>
    /// <param name="key">秘钥</param>
    /// <param name="dataEncoding">加载要加密的明文的编码方式</param>
    /// <param name="upperLower">密文值返回大小写</param>
    /// <returns></returns>
    public static string HMACSHA512Encrypt(string data, string key, Encoding dataEncoding, UpperLower upperLower)
    {
      StringBuilder sb = new StringBuilder();
      using (HMACSHA512 hmacsha512 = new HMACSHA512())
      {
        hmacsha512.Key = System.Text.Encoding.UTF8.GetBytes(key);
        byte[] dataBytes = dataEncoding.GetBytes(data);
        byte[] encryptBytes = hmacsha512.ComputeHash(dataBytes);
        for (int i = 0; i < encryptBytes.Length; i++)
        {
          sb.Append(upperLower == UpperLower.Upper ? encryptBytes[i].ToString("X2") : encryptBytes[i].ToString("x2"));
        }
        return sb.ToString();
      }
    }

    #endregion

  }

 

散列哈希加密

标签:ace   summary   build   hmac   lower   大小写   service   com   sys   

原文地址:https://www.cnblogs.com/tlmbem/p/10798865.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!