zoukankan      html  css  js  c++  java
  • MD5密码加密

      using System;

      using System.Security.Cryptography;

      using System.Text;

      namespace DimoNetwork.Common.DEncrypt

      {

      public enum MD5ResultMode : byte

      {

      Strong = 0,

      Weak = 1

      }

      ///

      /// 在应用程序中定义用于单向加密文本的方法

      ///

      public class TextEncrypt

      {

      private TextEncrypt()

      {

      }

      #region ========加密========

      ///

      /// 加密

      ///

      ///

      ///

      public static string Encrypt(string Text)

      {

      return Encrypt(Text, "DimoNet");

      }

      ///

      /// 加密数据

      ///

      ///

      ///

      ///

      public static string Encrypt(string Text, string sKey)

      {

      DESCryptoServiceProvider des = new DESCryptoServiceProvider();

      byte[] inputByteArray;

      inputByteArray = Encoding.Default.GetBytes(Text);

      des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));

      des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));

      System.IO.MemoryStream ms = new System.IO.MemoryStream();

      CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);

      cs.Write(inputByteArray, 0, inputByteArray.Length);

      cs.FlushFinalBlock();

      StringBuilder ret = new StringBuilder();

      foreach (byte b in ms.ToArray())

      {

      ret.AppendFormat("{0:X2}", b);

      }

      return ret.ToString();

      }

      ///

      /// MD5 加密

      ///

      ///要加密的字符串

      ///

      public static string MD5EncryptPassword(string password, int? length = null)

      {

      if (password == null)

      {

      throw new ArgumentNullException("password");

      }

      return MD5EncryptPassword(password, MD5ResultMode.Strong, length);

      }

      ///

      /// MD5 加密

      ///

      ///要加密的字符串

      ///加密强度

      ///

      public static string MD5EncryptPassword(string password, MD5ResultMode mode, int? length = null)

      {

      if (password == null)

      {

      throw new ArgumentNullException("password");

      }

      MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();

      string str = BitConverter.ToString(provider.ComputeHash(Encoding.UTF8.GetBytes(password)));

      if (length != null && length == 16)

      {

      str = BitConverter.ToString(provider.ComputeHash(Encoding.UTF8.GetBytes(password)), 4, 8);

      }

      provider.Clear();

      if (mode != MD5ResultMode.Strong)

      {

      return str.Replace("-", null).Substring(8, 0x10);

      }

      return str.Replace("-", null);

      }

      #endregion

      #region ========解密========

      ///

      /// 解密

      ///

      ///

      ///

      public static string Decrypt(string Text)

      {

      return Decrypt(Text, "DimoNet");

      }

      ///

      /// 解密数据

      ///

      ///

      ///

      ///

      public static string Decrypt(string Text, string sKey)

      {

      DESCryptoServiceProvider des = new DESCryptoServiceProvider();

      int len;

      len = Text.Length / 2;

      byte[] inputByteArray = new byte[len];

      int x, i;

      for (x = 0; x < len; x++)

      {

      // i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);

      i = System.Convert.ToInt32(Text.Substring(x * 2, 2), 16);

      inputByteArray[x] = (byte)i;

      }

      des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));

      des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));

      System.IO.MemoryStream ms = new System.IO.MemoryStream();

      CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);

      cs.Write(inputByteArray, 0, inputByteArray.Length);

      cs.FlushFinalBlock();

      return Encoding.Default.GetString(ms.ToArray());

      }

      #endregion

      ///

      /// Base64 解码

      ///

      ///

      ///

      public static string Base64Decode(string message)

      {

      byte[] bytes = Convert.FromBase64String(message);

      return Encoding.UTF8.GetString(bytes);

      }

      ///

      /// Base64 编码

      ///

      ///

      ///

      public static string Base64Encode(string message)

      {

      return Convert.ToBase64String(Encoding.UTF8.GetBytes(message));

      }

      ///

      /// DSA 加密

      ///

      ///要加密的字符串

      ///

      public static string DSAEncryptPassword(string password)

      {

      if (password == null)

      {

      throw new ArgumentNullException("password");

      }

      DSACryptoServiceProvider provider = new DSACryptoServiceProvider();

      string str = BitConverter.ToString(provider.SignData(Encoding.UTF8.GetBytes(password)));

      provider.Clear();

      return str.Replace("-", null);

      }

      ///

      /// MD5 加密

      ///

      ///要加密的字符串

      ///

      public static string EncryptPassword(string password)

      {

      if (password == null)

      {

      throw new ArgumentNullException("password");

      }

      return MD5EncryptPassword(password);

      }

      ///

      /// MD5 加密

      ///

      ///要加密的字符串

      ///

      public static string MD5EncryptPassword(string password)

      {

      if (password == null)

      {

      throw new ArgumentNullException("password");

      }

      return MD5EncryptPassword(password, MD5ResultMode.Strong);

      }

      ///

      /// MD5 加密

      ///

      ///要加密的字符串

      ///加密强度

      ///

      public static string MD5EncryptPassword(string password, MD5ResultMode mode)

      {

      if (password == null)

      {

      throw new ArgumentNullException("password");

      }

      MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();

      string str = BitConverter.ToString(provider.ComputeHash(Encoding.UTF8.GetBytes(password)));

      provider.Clear();

      if (mode != MD5ResultMode.Strong)

      {

      return str.Replace("-", null).Substring(8, 0x10);

      }

      return str.Replace("-", null);

      }

      ///

      /// SHA1 加密

      ///

      ///要加密的字符串

      ///

      public static string SHA1EncryptPassword(string password)

      {

      if (password == null)

      {

      throw new ArgumentNullException("password");

      }

      SHA1CryptoServiceProvider provider = new SHA1CryptoServiceProvider();

      string str = BitConverter.ToString(provider.ComputeHash(Encoding.UTF8.GetBytes(password)));

      provider.Clear();

      return str.Replace("-", null);

      }

      ///

      /// SHA256 加密

      ///

      ///要加密的字符串

      ///

      public static string SHA256(string password)

      {

      byte[] bytes = Encoding.UTF8.GetBytes(password);

      SHA256Managed managed = new SHA256Managed();

      return Convert.ToBase64String(managed.ComputeHash(bytes));

      }

      }

      }

      复制代码(编辑:雷林鹏 来源:网络)

  • 相关阅读:
    深度之眼PyTorch训练营第二期 --- 8、权值初始化
    深度之眼PyTorch训练营第二期 ---7、nn网络层--卷积层
    深度之眼PyTorch训练营第二期 ---6、nn.Module与网络模型构建
    C#数据库部分复习
    【jzoj1481】偷懒的西西
    【2020.12.03提高组模拟】黎明卿 (bondorudo)
    【2020.11.30提高组模拟】剪辣椒(chilli)
    引流
    【高精度】加减乘+组合数+比较大小(结构体)
    【易懂】费用流【SDOI2009】晨跑
  • 原文地址:https://www.cnblogs.com/pengpeng1208/p/9405362.html
Copyright © 2011-2022 走看看