Encrypt.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using System.Security.Cryptography;
  5. namespace SHJX.Service.Common.Utils
  6. {
  7. public class Encrypt
  8. {
  9. //默认密钥向量
  10. private static readonly byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
  11. /// <summary>
  12. /// DES加密字符串
  13. /// </summary>
  14. public static string EncryptDES(string encryptString, string encryptKey)
  15. {
  16. try
  17. {
  18. byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
  19. byte[] rgbIV = Keys;
  20. byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
  21. DESCryptoServiceProvider dCSP = new();
  22. MemoryStream mStream = new();
  23. CryptoStream cStream = new(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
  24. cStream.Write(inputByteArray, 0, inputByteArray.Length);
  25. cStream.FlushFinalBlock();
  26. return Convert.ToBase64String(mStream.ToArray());
  27. }
  28. catch
  29. {
  30. return encryptString;
  31. }
  32. }
  33. /// <summary>
  34. /// DES解密字符串
  35. /// </summary>
  36. public static string DecryptDES(string decryptString, string decryptKey)
  37. {
  38. try
  39. {
  40. byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
  41. byte[] rgbIV = Keys;
  42. byte[] inputByteArray = Convert.FromBase64String(decryptString);
  43. DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
  44. MemoryStream mStream = new MemoryStream();
  45. CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
  46. cStream.Write(inputByteArray, 0, inputByteArray.Length);
  47. cStream.FlushFinalBlock();
  48. return Encoding.UTF8.GetString(mStream.ToArray());
  49. }
  50. catch
  51. {
  52. return decryptString;
  53. }
  54. }
  55. /// <summary>
  56. /// MD5加密
  57. /// </summary>
  58. /// <param name="str">加密字符</param>
  59. /// <param name="code">加密位数16/32</param>
  60. /// <returns></returns>
  61. public static string GetMd5(string str, int Length = 32)
  62. {
  63. if (Length != 16 && Length != 32) throw new ArgumentException("Length参数无效,只能为16位或32位");
  64. MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();
  65. byte[] b = MD5.ComputeHash(Encoding.UTF8.GetBytes(str));
  66. StringBuilder StrB = new StringBuilder();
  67. for (int i = 0; i < b.Length; i++)
  68. StrB.Append(b[i].ToString("x").PadLeft(2, '0'));
  69. if (Length == 16)
  70. return StrB.ToString(8, 16);
  71. else
  72. return StrB.ToString();
  73. }
  74. /// <summary>
  75. /// 数据库密码解密
  76. /// </summary>
  77. /// <param name="decryptString"></param>
  78. /// <param name="key"></param>
  79. /// <returns></returns>
  80. public static string Decrypt_asc(string decryptString, string key)
  81. {
  82. string retstr = string.Empty;
  83. string tempStr, tStr;
  84. tempStr = tStr = string.Empty;
  85. string curdestr = decryptString;
  86. int keyPtr = 0;
  87. int keylen = key.Length;
  88. int sourceLen = decryptString.Length;
  89. int tempVal, tempKey;
  90. for (int i = 0; i < sourceLen; i++)
  91. {
  92. tempVal = Asc(Right(decryptString, decryptString.Length - i));
  93. tempKey = Asc(Right(key, keylen - keyPtr));
  94. tempVal -= tempKey;
  95. do
  96. {
  97. if (tempVal < 0)
  98. {
  99. tempVal += 255;
  100. }
  101. }
  102. while (tempVal < 0);
  103. tStr = Chr(tempVal);
  104. retstr += tStr;
  105. keyPtr++;
  106. if (keyPtr > keylen)
  107. {
  108. keyPtr = 0;
  109. }
  110. }
  111. return retstr;
  112. }
  113. public static string Right(string sSource, int iLength)
  114. {
  115. return sSource.Substring(iLength > sSource.Length ? 0 : sSource.Length - iLength);
  116. }
  117. public static string Decrypt_asc(string decryptString)
  118. {
  119. string retstr = string.Empty;
  120. string[] strlist = decryptString.Split('.');
  121. string curstr = string.Empty;
  122. foreach (string str in strlist)
  123. {
  124. if (!string.IsNullOrEmpty(str))
  125. {
  126. curstr += Convert.ToChar(Convert.ToInt32(str));
  127. }
  128. }
  129. retstr = Decrypt_asc(curstr, "5f945nfd721kdye95nsg327v3cb");
  130. return retstr;
  131. }
  132. public static int Asc(string character)
  133. {
  134. int intAsciiCode = 0;
  135. if (!string.IsNullOrEmpty(character))
  136. {
  137. //System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
  138. intAsciiCode = Convert.ToInt16(character.ToCharArray()[0]);
  139. }
  140. return intAsciiCode;
  141. }
  142. public static string Chr(int asciiCode)
  143. {
  144. if (asciiCode >= 0 && asciiCode <= 255)
  145. {
  146. System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
  147. byte[] byteArray = new byte[] { (byte)asciiCode };
  148. string strCharacter = asciiEncoding.GetString(byteArray);
  149. return (strCharacter);
  150. }
  151. else
  152. {
  153. throw new Exception("ASCII Code is not valid.");
  154. }
  155. }
  156. }
  157. }