MD5密码加密

using System;
using System.Security.Cryptography;
using System.Text;

namespace DimoNetwork.Common.DEncrypt
{
    public enum MD5ResultMode : byte
    {
        Strong = 0,
        Weak = 1
    }

    /// <summary>
    /// 在应用程序中定义用于单向加密文本的方法
    /// </summary>
    public class TextEncrypt
    {
        private TextEncrypt()
        {
        }

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

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>
        public static string Encrypt(string Text)
        {
            return Encrypt(Text, "DimoNet");
        }
        /// <summary> 
        /// 加密数据 
        /// </summary> 
        /// <param name="Text"></param> 
        /// <param name="sKey"></param> 
        /// <returns></returns> 
        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();
        }


        /// <summary>
        /// MD5 加密
        /// </summary>
        /// <param name="password">要加密的字符串</param>
        /// <returns></returns>
        public static string MD5EncryptPassword(string password, int? length = null)
        {
            if (password == null)
            {
                throw new ArgumentNullException("password");
            }
            return MD5EncryptPassword(password, MD5ResultMode.Strong, length);
        }

        /// <summary>
        /// MD5 加密
        /// </summary>
        /// <param name="password">要加密的字符串</param>
        /// <param name="mode">加密强度</param>
        /// <returns></returns>
        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 ========解密========


        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>
        public static string Decrypt(string Text)
        {
            return Decrypt(Text, "DimoNet");
        }
        /// <summary> 
        /// 解密数据 
        /// </summary> 
        /// <param name="Text"></param> 
        /// <param name="sKey"></param> 
        /// <returns></returns> 
        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 

        /// <summary>
        ///  Base64 解码
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static string Base64Decode(string message)
        {
            byte[] bytes = Convert.FromBase64String(message);
            return Encoding.UTF8.GetString(bytes);
        }
        /// <summary>
        /// Base64 编码
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static string Base64Encode(string message)
        {
            return Convert.ToBase64String(Encoding.UTF8.GetBytes(message));
        }
        /// <summary>
        ///  DSA 加密
        /// </summary>
        /// <param name="password">要加密的字符串</param>
        /// <returns></returns>
        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);
        }
        /// <summary>
        /// MD5 加密
        /// </summary>
        /// <param name="password">要加密的字符串</param>
        /// <returns></returns>
        public static string EncryptPassword(string password)
        {
            if (password == null)
            {
                throw new ArgumentNullException("password");
            }
            return MD5EncryptPassword(password);
        }
        /// <summary>
        /// MD5 加密
        /// </summary>
        /// <param name="password">要加密的字符串</param>
        /// <returns></returns>
        public static string MD5EncryptPassword(string password)
        {
            if (password == null)
            {
                throw new ArgumentNullException("password");
            }
            return MD5EncryptPassword(password, MD5ResultMode.Strong);
        }
        /// <summary>
        /// MD5 加密
        /// </summary>
        /// <param name="password">要加密的字符串</param>
        /// <param name="mode">加密强度</param>
        /// <returns></returns>
        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);
        }
        /// <summary>
        /// SHA1 加密
        /// </summary>
        /// <param name="password">要加密的字符串</param>
        /// <returns></returns>
        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);
        }
        /// <summary>
        /// SHA256 加密
        /// </summary>
        /// <param name="password">要加密的字符串</param>
        /// <returns></returns>
        public static string SHA256(string password)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(password);
            SHA256Managed managed = new SHA256Managed();
            return Convert.ToBase64String(managed.ComputeHash(bytes));
        }
    }
}

 

转载于:https://www.cnblogs.com/RambleLife/p/8889012.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值