CNAS取数仪器端升级
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

EncryptionOperation.cs 5.0KB

vor 4 Monaten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using log4net;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8. namespace CnasSynchronousCommon
  9. {
  10. public class EncryptionOperation
  11. {
  12. #region MD5加密
  13. /// <summary>
  14. /// MD5加密
  15. /// </summary>
  16. /// <param name="input"></param>
  17. /// <returns></returns>
  18. public static string GetMD5Hash(String input)
  19. {
  20. string cl = input;
  21. string pwd = "";
  22. MD5 md5 = MD5.Create();//实例化一个md5对像
  23. // 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择 
  24. byte[] s = md5.ComputeHash(Encoding.Default.GetBytes(cl));
  25. // 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
  26. for (int i = 0; i < s.Length; i++)
  27. {
  28. // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符
  29. pwd = pwd + s[i].ToString("X");
  30. }
  31. return pwd;
  32. }
  33. #endregion
  34. // 对称加密算法提供器
  35. private ICryptoTransform encryptor; // 加密器对象
  36. private ICryptoTransform decryptor; // 解密器对象
  37. private const int BufferSize = 1024;
  38. public EncryptionOperation(string algorithmName, string key)
  39. {
  40. try
  41. {
  42. SymmetricAlgorithm provider = SymmetricAlgorithm.Create(algorithmName);
  43. provider.Key = Encoding.UTF8.GetBytes(key);
  44. provider.IV = new byte[] { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
  45. encryptor = provider.CreateEncryptor();
  46. decryptor = provider.CreateDecryptor();
  47. }
  48. catch (Exception ex)
  49. {
  50. string s = ex.Message.ToString();
  51. }
  52. }
  53. public EncryptionOperation(string key) : this("TripleDES", key) { }
  54. // 加密算法
  55. public string Encrypt(string clearText)
  56. {
  57. try
  58. {
  59. // 创建明文流
  60. byte[] clearBuffer = Encoding.UTF8.GetBytes(clearText);
  61. MemoryStream clearStream = new MemoryStream(clearBuffer);
  62. // 创建空的密文流
  63. MemoryStream encryptedStream = new MemoryStream();
  64. CryptoStream cryptoStream =
  65. new CryptoStream(encryptedStream, encryptor, CryptoStreamMode.Write);
  66. // 将明文流写入到buffer中
  67. // 将buffer中的数据写入到cryptoStream中
  68. int bytesRead = 0;
  69. byte[] buffer = new byte[BufferSize];
  70. do
  71. {
  72. bytesRead = clearStream.Read(buffer, 0, BufferSize);
  73. cryptoStream.Write(buffer, 0, bytesRead);
  74. } while (bytesRead > 0);
  75. cryptoStream.FlushFinalBlock();
  76. // 获取加密后的文本
  77. buffer = encryptedStream.ToArray();
  78. string encryptedText = Convert.ToBase64String(buffer);
  79. return encryptedText;
  80. }
  81. catch (Exception ex)
  82. {
  83. AppLog.Error(ex.Message);
  84. return "";
  85. }
  86. }
  87. // 解密算法
  88. public string Decrypt(string encryptedText)
  89. {
  90. try
  91. {
  92. byte[] encryptedBuffer = Convert.FromBase64String(encryptedText);
  93. Stream encryptedStream = new MemoryStream(encryptedBuffer);
  94. MemoryStream clearStream = new MemoryStream();
  95. CryptoStream cryptoStream =
  96. new CryptoStream(encryptedStream, decryptor, CryptoStreamMode.Read);
  97. int bytesRead = 0;
  98. byte[] buffer = new byte[BufferSize];
  99. do
  100. {
  101. bytesRead = cryptoStream.Read(buffer, 0, BufferSize);
  102. clearStream.Write(buffer, 0, bytesRead);
  103. } while (bytesRead > 0);
  104. buffer = clearStream.GetBuffer();
  105. string clearText =
  106. Encoding.UTF8.GetString(buffer, 0, (int)clearStream.Length);
  107. return clearText;
  108. }
  109. catch (Exception ex)
  110. {
  111. AppLog.Error(ex.Message);
  112. return "";
  113. }
  114. }
  115. public static string Encrypt(string clearText, string key)
  116. {
  117. EncryptionOperation helper = new EncryptionOperation(key);
  118. return helper.Encrypt(clearText);
  119. //return clearText;
  120. }
  121. public static string Decrypt(string encryptedText, string key)
  122. {
  123. EncryptionOperation helper = new EncryptionOperation(key);
  124. return helper.Decrypt(encryptedText);
  125. //return encryptedText;
  126. }
  127. }
  128. }