AES_DES.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 MOKA_Factory_Tools
  9. {
  10. //处理加密解密
  11. public class AES_DES
  12. {
  13. //变量
  14. public static byte[] AESIV { get; set; } = new byte[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  15. public static string AESKey { get; set; } ="32Dip3h9VIR4IYac";
  16. /// <summary>
  17. /// 加密
  18. /// </summary>
  19. /// <param name="plainText"></param> 需要加密的字符串
  20. /// <param name="key"></param> 密钥
  21. /// <param name="IV"></param> 偏移向量
  22. /// <returns>
  23. /// 加密结果字符串
  24. /// </returns>
  25. public static string EncryptStringToBytes_Aes(string plainText, string key, byte[] IV)
  26. {
  27. try
  28. {
  29. byte[] Key = Encoding.UTF8.GetBytes(key);
  30. // Check arguments.
  31. if (plainText == null || plainText.Length <= 0)
  32. throw new ArgumentNullException("plainText");
  33. if (Key == null || Key.Length <= 0)
  34. throw new ArgumentNullException("Key");
  35. if (IV == null || IV.Length <= 0)
  36. throw new ArgumentNullException("IV");
  37. byte[] encrypted;
  38. using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
  39. {
  40. aesAlg.Key = Key;
  41. aesAlg.IV = IV;
  42. aesAlg.Mode = CipherMode.ECB;
  43. aesAlg.Padding = PaddingMode.PKCS7;
  44. ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
  45. using (MemoryStream msEncrypt = new MemoryStream())
  46. {
  47. using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
  48. {
  49. using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
  50. {
  51. swEncrypt.Write(plainText);
  52. }
  53. encrypted = msEncrypt.ToArray();
  54. }
  55. }
  56. }
  57. return Convert.ToBase64String(encrypted);
  58. }
  59. catch (Exception ex)
  60. {
  61. return ex.Message;
  62. }
  63. }
  64. /// <summary>
  65. /// 解密
  66. /// </summary>
  67. /// <param name="CipherText"></param> 需解密的字符串
  68. /// <param name="Key"></param> 密钥
  69. /// <param name="IV"></param> 偏移向量
  70. /// <returns>
  71. /// 解密结果字符串
  72. /// </returns>
  73. public static string DecryptStringFromBytes_Aes(string CipherText, string Key, byte[] IV)
  74. {
  75. try
  76. {
  77. byte[] cipherText = Convert.FromBase64String(CipherText);
  78. byte[] key = Encoding.UTF8.GetBytes(Key);
  79. if (cipherText == null || cipherText.Length <= 0)
  80. throw new ArgumentNullException("cipherText");
  81. if (key == null || key.Length <= 0)
  82. throw new ArgumentNullException("Key");
  83. if (IV == null || IV.Length <= 0)
  84. throw new ArgumentNullException("IV");
  85. string plaintext = null;
  86. using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
  87. {
  88. aesAlg.Key = key;
  89. aesAlg.IV = IV;
  90. aesAlg.Mode = CipherMode.ECB;
  91. aesAlg.Padding = PaddingMode.PKCS7;
  92. ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
  93. using (MemoryStream msDecrypt = new MemoryStream(cipherText))
  94. {
  95. using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
  96. {
  97. using (StreamReader srDecrypt = new StreamReader(csDecrypt))
  98. {
  99. plaintext = srDecrypt.ReadToEnd();
  100. }
  101. }
  102. }
  103. }
  104. return plaintext;
  105. }
  106. catch (Exception ex)
  107. {
  108. return ex.Message;
  109. }
  110. }
  111. }
  112. }