DesEncrypt.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. namespace CZFW.Core.Security
  7. {
  8. public class DesEncrypt
  9. {
  10. private const string DesKey = "CZCMS_desencrypt_2016";
  11. #region ========加密========
  12. /// <summary>
  13. /// 加密
  14. /// </summary>
  15. /// <param name="text"></param>
  16. /// <returns></returns>
  17. public static string Encrypt(string text)
  18. {
  19. return Encrypt(text, DesKey);
  20. }
  21. /// <summary>
  22. /// 加密数据
  23. /// </summary>
  24. /// <param name="text"></param>
  25. /// <param name="sKey"></param>
  26. /// <returns></returns>
  27. public static string Encrypt(string text, string sKey)
  28. {
  29. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  30. var inputByteArray = Encoding.Default.GetBytes(text);
  31. des.Key = Encoding.ASCII.GetBytes(GetMd5Hash(DesKey).Substring(0, 8));
  32. des.IV = Encoding.ASCII.GetBytes(GetMd5Hash(DesKey).Substring(0, 8));
  33. MemoryStream ms = new MemoryStream();
  34. CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
  35. cs.Write(inputByteArray, 0, inputByteArray.Length);
  36. cs.FlushFinalBlock();
  37. StringBuilder ret = new StringBuilder();
  38. foreach (byte b in ms.ToArray())
  39. {
  40. ret.AppendFormat("{0:X2}", b);
  41. }
  42. return ret.ToString();
  43. }
  44. #endregion
  45. #region ========解密========
  46. /// <summary>
  47. /// 解密
  48. /// </summary>
  49. /// <param name="text"></param>
  50. /// <returns></returns>
  51. public static string Decrypt(string text)
  52. {
  53. return !string.IsNullOrEmpty(text) ? Decrypt(text, DesKey) : "";
  54. }
  55. /// <summary>
  56. /// 解密数据
  57. /// </summary>
  58. /// <param name="text"></param>
  59. /// <param name="sKey"></param>
  60. /// <returns></returns>
  61. public static string Decrypt(string text, string sKey)
  62. {
  63. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  64. var len = text.Length / 2;
  65. byte[] inputByteArray = new byte[len];
  66. int x;
  67. for (x = 0; x < len; x++)
  68. {
  69. var i = Convert.ToInt32(text.Substring(x * 2, 2), 16);
  70. inputByteArray[x] = (byte)i;
  71. }
  72. des.Key = Encoding.ASCII.GetBytes(GetMd5Hash(DesKey).Substring(0, 8));
  73. des.IV = Encoding.ASCII.GetBytes(GetMd5Hash(DesKey).Substring(0, 8));
  74. MemoryStream ms = new MemoryStream();
  75. CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
  76. cs.Write(inputByteArray, 0, inputByteArray.Length);
  77. cs.FlushFinalBlock();
  78. return Encoding.Default.GetString(ms.ToArray());
  79. }
  80. public static string GetMd5Hash(string input)
  81. {
  82. MD5 md5Hash = MD5.Create();
  83. // Convert the input string to a byte array and compute the hash.
  84. byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
  85. // Create a new Stringbuilder to collect the bytes
  86. // and create a string.
  87. StringBuilder sBuilder = new StringBuilder();
  88. // Loop through each byte of the hashed data
  89. // and format each one as a hexadecimal string.
  90. for (int i = 0; i < data.Length; i++)
  91. {
  92. sBuilder.Append(data[i].ToString("X2"));
  93. }
  94. // Return the hexadecimal string.
  95. return sBuilder.ToString();
  96. }
  97. #endregion
  98. /// <summary>
  99. /// Base64加密
  100. /// </summary>
  101. /// <param name="encoding">编码格式</param>
  102. /// <param name="json">加密的字符串</param>
  103. /// <returns></returns>
  104. public static string Base64Encrypt(Encoding encoding,string json)
  105. {
  106. string encodeString = "";
  107. byte[] b = encoding.GetBytes(json);
  108. try {
  109. encodeString = Convert.ToBase64String(b);
  110. }
  111. catch (Exception ex)
  112. {
  113. encodeString = json;
  114. }
  115. return encodeString;
  116. }
  117. /// <summary>
  118. /// Base64解密
  119. /// </summary>
  120. /// <param name="encoding"></param>
  121. /// <param name="json"></param>
  122. /// <returns></returns>
  123. public static string BaseDecrypt(Encoding encoding, string json)
  124. {
  125. string decryptString = "";
  126. byte[] b = Convert.FromBase64String(json);
  127. try
  128. {
  129. decryptString = encoding.GetString(b);
  130. }
  131. catch (Exception ex)
  132. {
  133. decryptString = json;
  134. }
  135. return decryptString;
  136. }
  137. public static string byte2hex(byte[] abyte0)
  138. {
  139. StringBuilder sb = new StringBuilder();
  140. for (int i = 0; i<abyte0.Length; i++)
  141. {
  142. if ((abyte0[i] & 0xff) < 16)
  143. {
  144. sb.Append("0");
  145. }
  146. sb.Append(Convert.ToString((long)abyte0[i] & (long)255, 16));
  147. }
  148. return sb.ToString();
  149. }
  150. public string MD5Base64(byte[] data)
  151. {
  152. //MD5 md5 = new MD5CryptoServiceProvider();
  153. //byte[] md5Str = md5.ComputeHash(data);
  154. var md5 = MD5.Create();
  155. var bs = md5.ComputeHash(data);
  156. var str = Convert.ToBase64String(bs);
  157. return str;
  158. }
  159. /// <summary>
  160. /// 重写Java 16进制MD5加密
  161. /// </summary>
  162. /// <param name="json"></param>
  163. /// <returns></returns>
  164. public static string JavaMD5(string json)
  165. {
  166. MD5 md5Hash = MD5.Create();
  167. // Convert the input string to a byte array and compute the hash.
  168. byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(json));
  169. return byte2hex(data);
  170. }
  171. }
  172. }