/We7.CMS.Utils/EncryptString.cs

https://github.com/trycath/We7CMS · C# · 169 lines · 126 code · 11 blank · 32 comment · 19 complexity · 1a24b972569fff771b0cffc05ed29a18 MD5 · raw file

  1. using System;
  2. using System.IO;
  3. using System.Security;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. using We7.Framework.Config;
  7. namespace We7.CMS
  8. {
  9. public class EncryptString
  10. {
  11. /// <summary>
  12. /// DES加密字符串
  13. /// </summary>
  14. /// <param name="encryptString">待加密的字符串</param>
  15. /// <param name="rgbKey">加密密钥,要求为8位</param>
  16. /// <param name="rgbIV">密钥向量</param>
  17. /// <returns>加密成功返回加密后的字串,失败返Null</returns>
  18. public byte[] DES_Encrypt(string encryptString, byte[] rgbKey, byte[] rgbIV)
  19. {
  20. try
  21. {
  22. if (encryptString != null && encryptString != "")
  23. {
  24. byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
  25. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  26. MemoryStream memoryStream = new MemoryStream();
  27. CryptoStream cryptoStream = new CryptoStream(memoryStream, des.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
  28. cryptoStream.Write(inputByteArray, 0, inputByteArray.Length);
  29. cryptoStream.FlushFinalBlock();
  30. if (memoryStream != null)
  31. {
  32. return memoryStream.ToArray();
  33. }
  34. else
  35. {
  36. return null;
  37. }
  38. }
  39. else
  40. { return null; }
  41. }
  42. catch (Exception ex)
  43. {
  44. throw ex;
  45. //return null;
  46. }
  47. }
  48. /// <summary>
  49. /// DES解密字符串
  50. /// </summary>
  51. /// <param name= "decryptString "> 待解密的字符串 </param>
  52. /// <param name= "rgbKey "> 解密密钥,要求为8位,和加密密钥相同 </param>
  53. /// <param name= "rgbIV "> 密钥向量 </param>
  54. /// <returns> 解密成功返回解密后的字符串,失败返源字符串 </returns>
  55. public string DES_Decrypt(byte[] decryptByteArray, byte[] rgbKey, byte[] rgbIV)
  56. {
  57. try
  58. {
  59. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  60. MemoryStream memoryStream = new MemoryStream();
  61. CryptoStream cryptoStream = new CryptoStream(memoryStream, des.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
  62. cryptoStream.Write(decryptByteArray, 0, decryptByteArray.Length);
  63. cryptoStream.FlushFinalBlock();
  64. if (memoryStream != null)
  65. {
  66. return Encoding.UTF8.GetString(memoryStream.ToArray());
  67. }
  68. else
  69. {
  70. return "False";
  71. }
  72. }
  73. catch (Exception ex)
  74. {
  75. throw ex;
  76. }
  77. }
  78. private static string skey = "xbdongli";
  79. public static string Encrypt(string pToEncrypt)
  80. {
  81. return pToEncrypt;
  82. GeneralConfigInfo ci = GeneralConfigs.GetConfig();
  83. if (ci != null && ci.JiaMiKey.Length == 8)
  84. {
  85. skey = ci.JiaMiKey;
  86. }
  87. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  88. //把字符串放到byte数组中
  89. //原来使用的UTF8编码,我改成Unicode编码了,不行
  90. if (pToEncrypt == null)
  91. {
  92. return "";
  93. throw new Exception();
  94. }
  95. byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
  96. //byte[] inputByteArray=Encoding.Unicode.GetBytes(pToEncrypt);
  97. //建立加密对象的密钥和偏移量
  98. //原文使用ASCIIEncoding.ASCII方法的GetBytes方法
  99. //使得输入密码必须输入英文文本
  100. des.Key = ASCIIEncoding.ASCII.GetBytes(skey);
  101. des.IV = ASCIIEncoding.ASCII.GetBytes(skey);
  102. MemoryStream ms = new MemoryStream();
  103. CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
  104. //Write the byte array into the crypto stream
  105. //(It will end up in the memory stream)
  106. cs.Write(inputByteArray, 0, inputByteArray.Length);
  107. cs.FlushFinalBlock();
  108. //Get the data back from the memory stream, and into a string
  109. StringBuilder ret = new StringBuilder();
  110. foreach (byte b in ms.ToArray())
  111. {
  112. //Format as hex
  113. ret.AppendFormat("{0:X2}", b);
  114. }
  115. ret.ToString();
  116. return ret.ToString();
  117. }
  118. //解密方法
  119. public static string Decrypt(string pToDecrypt)
  120. {
  121. return pToDecrypt;
  122. GeneralConfigInfo ci = GeneralConfigs.GetConfig();
  123. if (ci != null && ci.JiaMiKey.Length == 8)
  124. {
  125. skey = ci.JiaMiKey;
  126. }
  127. try
  128. {
  129. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  130. //Put the input string into the byte array
  131. byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
  132. for (int x = 0; x < pToDecrypt.Length / 2; x++)
  133. {
  134. int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
  135. inputByteArray[x] = (byte)i;
  136. }
  137. //建立加密对象的密钥和偏移量,此值重要,不能修改
  138. des.Key = ASCIIEncoding.ASCII.GetBytes(skey);
  139. des.IV = ASCIIEncoding.ASCII.GetBytes(skey);
  140. MemoryStream ms = new MemoryStream();
  141. CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
  142. //Flush the data through the crypto stream into the memory stream
  143. cs.Write(inputByteArray, 0, inputByteArray.Length);
  144. cs.FlushFinalBlock();
  145. //Get the decrypted data back from the memory stream
  146. //建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象
  147. StringBuilder ret = new StringBuilder();
  148. return System.Text.Encoding.Default.GetString(ms.ToArray());
  149. }
  150. catch (Exception ex)
  151. {
  152. //throw ex;
  153. System.Web.HttpContext.Current.Response.Redirect("/Nonexistence.htm");
  154. return "";
  155. }
  156. }
  157. }
  158. }