• 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));

      }

      }

      }

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

  • 相关阅读:
    (转)ELK Stack 中文指南--性能优化
    (转)如何在CentOS / RHEL 7上安装Elasticsearch,Logstash和Kibana(ELK)
    (转)GlusterFS 01 理论基础,企业实战,故障处理
    (转)CentOS7.4环境下搭建--Gluster分布式集群存储
    (转)DB2性能优化 – 如何通过调整锁参数优化锁升级
    (转)架构师之DNS实战CentOS7VSCentOS6
    PHP:计算文件或数组中单词出现频率
    [获取行数]php读取大文件提供性能的方法,PHP的stream_get_line函数读取大文件获取文件的行数的方...
    Windows下配置环境变量和需不需要重启问题
    CENTOS 下安装APK反编译工具 APKTOOL
  • 原文地址:https://www.cnblogs.com/pengpeng1208/p/9405362.html
Copyright © 2020-2023  润新知