EncryptionHelper.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace PacsView3D
  9. {
  10. public enum EncryptionKeyEnum
  11. {
  12. KeyA,
  13. KeyB
  14. }
  15. public class EncryptionHelper
  16. {
  17. string encryptionKeyA = "xie_Nasb";
  18. string encryptionKeyB = "liuyeQJJ";
  19. string md5Begin = "Tianxia";
  20. string md5End = "Zhangjie";
  21. string encryptionKey = string.Empty;
  22. public EncryptionHelper()
  23. {
  24. this.InitKey();
  25. }
  26. public EncryptionHelper(EncryptionKeyEnum key)
  27. {
  28. this.InitKey(key);
  29. }
  30. private void InitKey(EncryptionKeyEnum key = EncryptionKeyEnum.KeyA)
  31. {
  32. switch (key)
  33. {
  34. case EncryptionKeyEnum.KeyA:
  35. encryptionKey = encryptionKeyA;
  36. break;
  37. case EncryptionKeyEnum.KeyB:
  38. encryptionKey = encryptionKeyB;
  39. break;
  40. }
  41. }
  42. public string EncryptString(string str)
  43. {
  44. return Encrypt(str, encryptionKey);
  45. }
  46. public string DecryptString(string str)
  47. {
  48. return Decrypt(str, encryptionKey);
  49. }
  50. public string GetMD5String(string str)
  51. {
  52. str = string.Concat(md5Begin, str, md5End);
  53. MD5 md5 = new MD5CryptoServiceProvider();
  54. byte[] fromData = Encoding.Unicode.GetBytes(str);
  55. byte[] targetData = md5.ComputeHash(fromData);
  56. string md5String = string.Empty;
  57. foreach (var b in targetData)
  58. md5String += b.ToString("x2");
  59. return md5String;
  60. }
  61. private string Encrypt(string str, string sKey)
  62. {
  63. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  64. byte[] inputByteArray = Encoding.Default.GetBytes(str);
  65. des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  66. des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  67. MemoryStream ms = new MemoryStream();
  68. CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
  69. cs.Write(inputByteArray, 0, inputByteArray.Length);
  70. cs.FlushFinalBlock();
  71. StringBuilder ret = new StringBuilder();
  72. foreach (byte b in ms.ToArray())
  73. {
  74. ret.AppendFormat("{0:X2}", b);
  75. }
  76. ret.ToString();
  77. return ret.ToString();
  78. }
  79. private string Decrypt(string pToDecrypt, string sKey)
  80. {
  81. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  82. byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
  83. for (int x = 0; x < pToDecrypt.Length / 2; x++)
  84. {
  85. int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
  86. inputByteArray[x] = (byte)i;
  87. }
  88. des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  89. des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  90. MemoryStream ms = new MemoryStream();
  91. CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
  92. cs.Write(inputByteArray, 0, inputByteArray.Length);
  93. cs.FlushFinalBlock();
  94. StringBuilder ret = new StringBuilder();
  95. return System.Text.Encoding.Default.GetString(ms.ToArray());
  96. }
  97. }
  98. }