DecodeEncrypt.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Security.Cryptography;
  6. using System.IO;
  7. namespace Ant.Common
  8. {
  9. public class DecodeEncrypt
  10. {
  11. #region ========解密一========
  12. //默认密钥向量
  13. //private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
  14. private static byte[] Keys = { 0x41, 0x72, 0x65, 0x79, 0x6F, 0x75, 0x6D, 0x79, 0x53, 0x6E, 0x6F, 0x77, 0x6D, 0x61, 0x6E, 0x3F };
  15. /// <summary>
  16. /// 解密的key 与加密key相同
  17. /// </summary>
  18. private static string decryptKey = "36CBB25BB63A43E9B7399EAE796F41B9";
  19. /// <summary>
  20. /// AES解密
  21. /// </summary>
  22. /// <param name="decryptString">需要解密的字符串</param>
  23. /// <param name="decryptKey">需要解密的Key</param>
  24. /// <returns></returns>
  25. public static string Decode(string decryptString)
  26. {
  27. try
  28. {
  29. decryptKey = CommonUtils.GetSubString(decryptKey, 32, "");
  30. decryptKey = decryptKey.PadRight(32, ' ');
  31. RijndaelManaged rijndaelProvider = new RijndaelManaged();
  32. rijndaelProvider.Key = Encoding.UTF8.GetBytes(decryptKey);
  33. rijndaelProvider.IV = Keys;
  34. ICryptoTransform rijndaelDecrypt = rijndaelProvider.CreateDecryptor();
  35. byte[] inputData = Convert.FromBase64String(decryptString);
  36. byte[] decryptedData = rijndaelDecrypt.TransformFinalBlock(inputData, 0, inputData.Length);
  37. return Encoding.UTF8.GetString(decryptedData);
  38. }
  39. catch
  40. {
  41. return "";
  42. }
  43. }
  44. #endregion
  45. #region ========解密二========
  46. //默认密钥向量
  47. private static byte[] DESKeys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
  48. /// <summary>
  49. /// 解密的key
  50. /// </summary>
  51. private static string DESdecryptKey = "!@#$%^&*";
  52. /// <summary>
  53. /// DES解密字符串
  54. /// </summary>
  55. /// <param name="decryptString">待解密的字符串</param>
  56. /// <returns>解密成功返回解密后的字符串,失败返源串</returns>
  57. public static string DESDecode(string decryptString)
  58. {
  59. try
  60. {
  61. DESdecryptKey = CommonUtils.GetSubString(DESdecryptKey, 8, "");
  62. DESdecryptKey = DESdecryptKey.PadRight(8, ' ');
  63. byte[] rgbKey = Encoding.UTF8.GetBytes(DESdecryptKey);
  64. byte[] rgbIV = DESKeys;
  65. byte[] inputByteArray = Convert.FromBase64String(decryptString);
  66. DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
  67. MemoryStream mStream = new MemoryStream();
  68. CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
  69. cStream.Write(inputByteArray, 0, inputByteArray.Length);
  70. cStream.FlushFinalBlock();
  71. return Encoding.UTF8.GetString(mStream.ToArray());
  72. }
  73. catch
  74. {
  75. return "";
  76. }
  77. }
  78. #endregion
  79. #region ========解密三========
  80. /// <summary>
  81. /// 解密
  82. /// </summary>
  83. /// <param name="Text"></param>
  84. /// <returns></returns>
  85. public static string Decrypt(string Text)
  86. {
  87. return Decrypt(Text, "jijianguo");
  88. }
  89. /// <summary>
  90. /// 解密数据
  91. /// </summary>
  92. /// <param name="Text"></param>
  93. /// <param name="sKey"></param>
  94. /// <returns></returns>
  95. public static string Decrypt(string Text, string sKey)
  96. {
  97. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  98. int len;
  99. len = Text.Length / 2;
  100. byte[] inputByteArray = new byte[len];
  101. int x, i;
  102. for (x = 0; x < len; x++)
  103. {
  104. i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
  105. inputByteArray[x] = (byte)i;
  106. }
  107. des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  108. des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  109. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  110. CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
  111. cs.Write(inputByteArray, 0, inputByteArray.Length);
  112. cs.FlushFinalBlock();
  113. return Encoding.Default.GetString(ms.ToArray());
  114. }
  115. #endregion
  116. }
  117. }