SecurityUtil.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. namespace Wms.Screen.Util.Comm
  7. {
  8. /// <summary>
  9. /// MD5加密类
  10. /// </summary>
  11. public class SecurityUtil
  12. {
  13. #region MD5加密
  14. /// <summary>
  15. /// 给一个字符串进行MD5加密
  16. /// </summary>
  17. /// <param name="strText">待加密字符串</param>
  18. /// <returns>加密后的字符串</returns>
  19. public static string MD5Encrypt(string strText)
  20. {
  21. MD5 md5 = new MD5CryptoServiceProvider();
  22. byte[] result = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(strText));
  23. return System.Text.Encoding.Default.GetString(result);
  24. }
  25. /// <summary>
  26. /// MD5加密
  27. /// </summary>
  28. /// <param name="str">加密字符</param>
  29. /// <param name="code">加密位数16/32</param>
  30. /// <returns></returns>
  31. public static string MD5(string str, int code)
  32. {
  33. MD5CryptoServiceProvider crypto = new MD5CryptoServiceProvider();
  34. byte[] bytes = Encoding.UTF7.GetBytes(str);
  35. bytes = crypto.ComputeHash(bytes);
  36. StringBuilder sb = new StringBuilder();
  37. foreach (byte num in bytes)
  38. {
  39. sb.AppendFormat("{0:x2}", num);
  40. }
  41. if (code == 16)
  42. {
  43. return sb.ToString().Substring(8, 16);
  44. }
  45. else
  46. {
  47. return sb.ToString();
  48. }
  49. }
  50. #endregion
  51. #region DES加密
  52. #region ========加密========
  53. /// <summary>
  54. /// 加密
  55. /// </summary>
  56. /// <param name="Text"></param>
  57. /// <returns></returns>
  58. public static string Encrypt(string Text)
  59. {
  60. return Encrypt(Text, "BOZHON.COM###");
  61. }
  62. /// <summary>
  63. /// 加密数据
  64. /// </summary>
  65. /// <param name="Text"></param>
  66. /// <param name="sKey"></param>
  67. /// <returns></returns>
  68. public static string Encrypt(string Text, string sKey)
  69. {
  70. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  71. byte[] inputByteArray;
  72. inputByteArray = Encoding.Default.GetBytes(Text);
  73. des.Key = ASCIIEncoding.ASCII.GetBytes(MD5Encrypt(sKey).Substring(0, 8));
  74. des.IV = ASCIIEncoding.ASCII.GetBytes(MD5Encrypt(sKey).Substring(0, 8));
  75. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  76. CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
  77. cs.Write(inputByteArray, 0, inputByteArray.Length);
  78. cs.FlushFinalBlock();
  79. StringBuilder ret = new StringBuilder();
  80. foreach (byte b in ms.ToArray())
  81. {
  82. ret.AppendFormat("{0:X2}", b);
  83. }
  84. return ret.ToString();
  85. }
  86. #endregion
  87. #region ========解密========
  88. /// <summary>
  89. /// 解密
  90. /// </summary>
  91. /// <param name="Text"></param>
  92. /// <returns></returns>
  93. public static string Decrypt(string Text)
  94. {
  95. if (!string.IsNullOrEmpty(Text))
  96. {
  97. return Decrypt(Text, "BOZHON.COM###");
  98. }
  99. else
  100. {
  101. return "";
  102. }
  103. }
  104. /// <summary>
  105. /// 解密数据
  106. /// </summary>
  107. /// <param name="Text"></param>
  108. /// <param name="sKey"></param>
  109. /// <returns></returns>
  110. public static string Decrypt(string Text, string sKey)
  111. {
  112. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  113. int len;
  114. len = Text.Length / 2;
  115. byte[] inputByteArray = new byte[len];
  116. int x, i;
  117. for (x = 0; x < len; x++)
  118. {
  119. i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
  120. inputByteArray[x] = (byte)i;
  121. }
  122. des.Key = ASCIIEncoding.ASCII.GetBytes(MD5Encrypt(sKey).Substring(0, 8));
  123. des.IV = ASCIIEncoding.ASCII.GetBytes(MD5Encrypt(sKey).Substring(0, 8));
  124. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  125. CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
  126. cs.Write(inputByteArray, 0, inputByteArray.Length);
  127. cs.FlushFinalBlock();
  128. return Encoding.Default.GetString(ms.ToArray());
  129. }
  130. #endregion
  131. #endregion
  132. #region Base64加密
  133. /// <summary>
  134. /// Base64加密,采用utf8编码方式加密
  135. /// </summary>
  136. /// <param name="source">待加密的明文</param>
  137. /// <returns>加密后的字符串</returns>
  138. public static string Base64Encode(string source)
  139. {
  140. return Base64Encode(Encoding.UTF8, source);
  141. }
  142. /// <summary>
  143. /// Base64加密
  144. /// </summary>
  145. /// <param name="encodeType">加密采用的编码方式</param>
  146. /// <param name="source">待加密的明文</param>
  147. /// <returns></returns>
  148. public static string Base64Encode(Encoding encodeType, string source)
  149. {
  150. string encode = string.Empty;
  151. byte[] bytes = encodeType.GetBytes(source);
  152. try
  153. {
  154. encode = Convert.ToBase64String(bytes);
  155. }
  156. catch
  157. {
  158. encode = source;
  159. }
  160. return encode;
  161. }
  162. /// <summary>
  163. /// Base64解密,采用utf8编码方式解密
  164. /// </summary>
  165. /// <param name="result">待解密的密文</param>
  166. /// <returns>解密后的字符串</returns>
  167. public static string Base64Decode(string result)
  168. {
  169. return Base64Decode(Encoding.UTF8, result);
  170. }
  171. /// <summary>
  172. /// Base64解密
  173. /// </summary>
  174. /// <param name="encodeType">解密采用的编码方式,注意和加密时采用的方式一致</param>
  175. /// <param name="result">待解密的密文</param>
  176. /// <returns>解密后的字符串</returns>
  177. public static string Base64Decode(Encoding encodeType, string result)
  178. {
  179. string decode = string.Empty;
  180. byte[] bytes = Convert.FromBase64String(result);
  181. try
  182. {
  183. decode = encodeType.GetString(bytes);
  184. }
  185. catch
  186. {
  187. decode = result;
  188. }
  189. return decode;
  190. }
  191. }
  192. #endregion
  193. }