aes.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Security.Cryptography;
  7. using System.IO;
  8. namespace Welling_Motor_Debug_Tool
  9. {
  10. internal class aes
  11. {
  12. // 加密字符串并保存到文件
  13. public static void EncryptToFile(string plainText, string filePath, string key, string iv)
  14. {
  15. using (Aes aesAlg = Aes.Create())
  16. {
  17. aesAlg.Key = Encoding.UTF8.GetBytes(key); // 密钥(必须是 16、24 或 32 字节)
  18. aesAlg.IV = Encoding.UTF8.GetBytes(iv); // 初始化向量(IV,必须是 16 字节)
  19. using (FileStream fsEncrypt = new FileStream(filePath, FileMode.Create))
  20. using (CryptoStream csEncrypt = new CryptoStream(fsEncrypt, aesAlg.CreateEncryptor(), CryptoStreamMode.Write))
  21. using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
  22. {
  23. swEncrypt.Write(plainText);
  24. }
  25. }
  26. }
  27. // 从加密文件解密刺符传
  28. public static string DecryptFromFile(string filePath, string key, string iv)
  29. {
  30. using (Aes aesAlg = Aes.Create())
  31. {
  32. aesAlg.Key = Encoding.UTF8.GetBytes(key); // 密钥(必须是 16、24 或 32 字节)
  33. aesAlg.IV = Encoding.UTF8.GetBytes(iv); // 初始化向量(IV,必须是 16 字节)
  34. using (FileStream fsDecrypt = new FileStream(filePath, FileMode.Open))
  35. using (CryptoStream csDecrypt = new CryptoStream(fsDecrypt, aesAlg.CreateDecryptor(), CryptoStreamMode.Read))
  36. using (StreamReader srDecrypt = new StreamReader(csDecrypt))
  37. {
  38. return srDecrypt.ReadToEnd();
  39. }
  40. }
  41. }
  42. // 加密字符串
  43. public static string EncryptString(string plainText, string key, string iv)
  44. {
  45. byte[] array;
  46. using (Aes aesAlg = Aes.Create())
  47. {
  48. aesAlg.Key = Encoding.UTF8.GetBytes(key); // 密钥(必须是 16、24 或 32 字节)
  49. aesAlg.IV = Encoding.UTF8.GetBytes(iv); // 初始化向量(IV,必须是 16 字节)
  50. ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
  51. using (MemoryStream memoryStream = new MemoryStream())
  52. {
  53. using (CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, encryptor, CryptoStreamMode.Write))
  54. {
  55. using (StreamWriter streamWriter = new StreamWriter((Stream)cryptoStream))
  56. {
  57. streamWriter.Write(plainText);
  58. }
  59. array = memoryStream.ToArray();
  60. }
  61. }
  62. }
  63. return Convert.ToBase64String(array);
  64. }
  65. public static string DecryptString(string cipherText, string key, string iv)
  66. {
  67. byte[] buffer = Convert.FromBase64String(cipherText);
  68. using (Aes aesAlg = Aes.Create())
  69. {
  70. aesAlg.Key = Encoding.UTF8.GetBytes(key); // 密钥(必须是 16、24 或 32 字节)
  71. aesAlg.IV = Encoding.UTF8.GetBytes(iv); // 初始化向量(IV,必须是 16 字节)
  72. ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
  73. using (MemoryStream memoryStream = new MemoryStream(buffer))
  74. {
  75. using (CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, decryptor, CryptoStreamMode.Read))
  76. {
  77. using (StreamReader streamReader = new StreamReader((Stream)cryptoStream))
  78. {
  79. return streamReader.ReadToEnd();
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }