123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Security.Cryptography;
- using System.Text;
- namespace Wms.Screen.Util.Comm
- {
- /// <summary>
- /// MD5加密类
- /// </summary>
- public class SecurityUtil
- {
- #region MD5加密
- /// <summary>
- /// 给一个字符串进行MD5加密
- /// </summary>
- /// <param name="strText">待加密字符串</param>
- /// <returns>加密后的字符串</returns>
- public static string MD5Encrypt(string strText)
- {
- MD5 md5 = new MD5CryptoServiceProvider();
- byte[] result = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(strText));
- return System.Text.Encoding.Default.GetString(result);
- }
- /// <summary>
- /// MD5加密
- /// </summary>
- /// <param name="str">加密字符</param>
- /// <param name="code">加密位数16/32</param>
- /// <returns></returns>
- public static string MD5(string str, int code)
- {
- MD5CryptoServiceProvider crypto = new MD5CryptoServiceProvider();
- byte[] bytes = Encoding.UTF7.GetBytes(str);
- bytes = crypto.ComputeHash(bytes);
- StringBuilder sb = new StringBuilder();
- foreach (byte num in bytes)
- {
- sb.AppendFormat("{0:x2}", num);
- }
- if (code == 16)
- {
- return sb.ToString().Substring(8, 16);
- }
- else
- {
- return sb.ToString();
- }
- }
- #endregion
- #region DES加密
- #region ========加密========
- /// <summary>
- /// 加密
- /// </summary>
- /// <param name="Text"></param>
- /// <returns></returns>
- public static string Encrypt(string Text)
- {
- return Encrypt(Text, "BOZHON.COM###");
- }
- /// <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(MD5Encrypt(sKey).Substring(0, 8));
- des.IV = ASCIIEncoding.ASCII.GetBytes(MD5Encrypt(sKey).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();
- }
- #endregion
- #region ========解密========
- /// <summary>
- /// 解密
- /// </summary>
- /// <param name="Text"></param>
- /// <returns></returns>
- public static string Decrypt(string Text)
- {
- if (!string.IsNullOrEmpty(Text))
- {
- return Decrypt(Text, "BOZHON.COM###");
- }
- else
- {
- return "";
- }
- }
- /// <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);
- inputByteArray[x] = (byte)i;
- }
- des.Key = ASCIIEncoding.ASCII.GetBytes(MD5Encrypt(sKey).Substring(0, 8));
- des.IV = ASCIIEncoding.ASCII.GetBytes(MD5Encrypt(sKey).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
- #endregion
- #region Base64加密
- /// <summary>
- /// Base64加密,采用utf8编码方式加密
- /// </summary>
- /// <param name="source">待加密的明文</param>
- /// <returns>加密后的字符串</returns>
- public static string Base64Encode(string source)
- {
- return Base64Encode(Encoding.UTF8, source);
- }
- /// <summary>
- /// Base64加密
- /// </summary>
- /// <param name="encodeType">加密采用的编码方式</param>
- /// <param name="source">待加密的明文</param>
- /// <returns></returns>
- public static string Base64Encode(Encoding encodeType, string source)
- {
- string encode = string.Empty;
- byte[] bytes = encodeType.GetBytes(source);
- try
- {
- encode = Convert.ToBase64String(bytes);
- }
- catch
- {
- encode = source;
- }
- return encode;
- }
- /// <summary>
- /// Base64解密,采用utf8编码方式解密
- /// </summary>
- /// <param name="result">待解密的密文</param>
- /// <returns>解密后的字符串</returns>
- public static string Base64Decode(string result)
- {
- return Base64Decode(Encoding.UTF8, result);
- }
- /// <summary>
- /// Base64解密
- /// </summary>
- /// <param name="encodeType">解密采用的编码方式,注意和加密时采用的方式一致</param>
- /// <param name="result">待解密的密文</param>
- /// <returns>解密后的字符串</returns>
- public static string Base64Decode(Encoding encodeType, string result)
- {
- string decode = string.Empty;
- byte[] bytes = Convert.FromBase64String(result);
- try
- {
- decode = encodeType.GetString(bytes);
- }
- catch
- {
- decode = result;
- }
- return decode;
- }
- }
- #endregion
- }
|