C# MD5加密字符串方法

    /// <summary>
    /// 32位 小写 MD5加密
    /// </summary>
    public static string MD5(string Str, bool isUpper = false)
    {
        var bytes = new MD5CryptoServiceProvider().ComputeHash(System.Text.Encoding.UTF8.GetBytes(Str));
        string ret = "";
        foreach (byte bb in bytes) { ret += Convert.ToString(bb, 16).PadLeft(2, '0'); }
        var result = ret.PadLeft(32, '0');
        return isUpper ? result.ToUpper() : result.ToLower();
    }

    /// <summary>
    /// 16位 小写 MD5加密
    /// </summary>
    /// <param name="Str"></param>
    /// <returns></returns>
    public static string MD5_16(string Str, bool isUpper = false)
    {
        var result = BitConverter.ToString(new MD5CryptoServiceProvider().ComputeHash(System.Text.Encoding.UTF8.GetBytes(Str)), 4, 8).Replace("-", "");
        return isUpper ? result.ToUpper() : result.ToLower();
    }
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security.Cryptography;


public class Des
{

    const string KEY_64 = "";
    const string IV_64 = ""; //注意了,是8个字符,64位

    public static string Encode(string data)
    {
        byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
        byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);

        DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
        int i = cryptoProvider.KeySize;
        MemoryStream ms = new MemoryStream();
        CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);

        StreamWriter sw = new StreamWriter(cst);
        sw.Write(data);
        sw.Flush();
        cst.FlushFinalBlock();
        sw.Flush();
        return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);

    }

    public static string Decode(string data)
    {
        byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
        byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);

        byte[] byEnc;
        try
        {
            byEnc = Convert.FromBase64String(string.Format(@data));
        }
        catch
        {
            return null;
        }

        DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
        MemoryStream ms = new MemoryStream(byEnc);
        CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
        StreamReader sr = new StreamReader(cst);
        return sr.ReadToEnd();
    }


    #region //DES

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

    /// <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 = System.Text.Encoding.UTF8.GetBytes(MD5(sKey).ToUpper().Substring(0, 8));
        des.IV = des.Key;
        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();
    }

    #endregion

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

    /// <summary> 
    /// 解密数据 
    /// </summary> 
    /// <param name="Text"></param> 
    /// <param name="sKey"></param> 
    /// <returns></returns> 
    public static string Decrypt(string Text, string sKey = "")
    {
        try
        {
            if (string.IsNullOrEmpty(Text)) return string.Empty;
            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);
                inputByteArray[x] = (byte)i;
            }
            des.Key = System.Text.Encoding.UTF8.GetBytes(MD5(sKey).ToUpper().Substring(0, 8));
            des.IV = des.Key;
            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());

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        return "";
    }
    #endregion
    #endregion

    #region //MD5

    /// <summary>
    /// 32位 小写 MD5加密
    /// </summary>
    public static string MD5(string Str, bool isUpper = false)
    {
        var bytes = new MD5CryptoServiceProvider().ComputeHash(System.Text.Encoding.UTF8.GetBytes(Str));
        string ret = "";
        foreach (byte bb in bytes) { ret += Convert.ToString(bb, 16).PadLeft(2, '0'); }
        var result = ret.PadLeft(32, '0');
        return isUpper ? result.ToUpper() : result.ToLower();
    }

    /// <summary>
    /// 16位 小写 MD5加密
    /// </summary>
    /// <param name="Str"></param>
    /// <returns></returns>
    public static string MD5_16(string Str, bool isUpper = false)
    {
        var result = BitConverter.ToString(new MD5CryptoServiceProvider().ComputeHash(System.Text.Encoding.UTF8.GetBytes(Str)), 4, 8).Replace("-", "");
        return isUpper ? result.ToUpper() : result.ToLower();
    }
    #endregion

}

 

posted on 2021-04-02 18:00  粗狂的_蜗牛  阅读(608)  评论(0编辑  收藏  举报