MD5加解密

来源:互联网 发布:最新域名页面访问升级 编辑:程序博客网 时间:2024/05/05 05:10
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Security.Cryptography;using System.Text;using System.Threading.Tasks;namespace RongGuang.Extend{   public  class MD5    {        public static string GenerateKey()        {            DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();            return ASCIIEncoding.ASCII.GetString(desCrypto.Key);        }        #region MD5加密        /// <summary>        /// MD5加密        /// </summary>        /// <param name="pToEncrypt"></param>        /// <param name="sKey"></param>        /// <returns></returns>        public static string MD5Encrypt(string pToEncrypt, string sKey)        {            DESCryptoServiceProvider des = new DESCryptoServiceProvider();            byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);            des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);            des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);            MemoryStream ms = new 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);            }            ret.ToString();            return ret.ToString();        }        #endregion        #region MD5解密        /// <summary>              /// MD5解密        /// </summary>        /// <param name="pToDecrypt"></param>        /// <param name="sKey"></param>        /// <returns></returns>        public static string MD5Decrypt(string pToDecrypt, string sKey)        {            DESCryptoServiceProvider des = new DESCryptoServiceProvider();            byte[] inputByteArray = new byte[pToDecrypt.Length / 2];            for (int x = 0; x < pToDecrypt.Length / 2; x++)            {                int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));                inputByteArray[x] = (byte)i;            }            des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);            des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);            MemoryStream ms = new MemoryStream();            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);            cs.Write(inputByteArray, 0, inputByteArray.Length);            cs.FlushFinalBlock();            StringBuilder ret = new StringBuilder();            return System.Text.Encoding.Default.GetString(ms.ToArray());        }        #endregion    }}
0 0