AES.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System.IO;
  2. using System.Security.Cryptography;
  3. namespace YOLODetectProcessLib
  4. {
  5. /// <summary>
  6. /// 用AES法加密解密
  7. /// </summary>
  8. public class AES
  9. {
  10. private static byte[] _iv = new byte[] { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
  11. private static byte[] _key = new byte[] { 0x11, 0x21, 0x12, 0x22, 0x31, 0x32, 0x02, 0x03, 0x11, 0x21, 0x12, 0x22, 0x31, 0x32, 0x02, 0x03 };
  12. public static byte[] AESEncrypt(byte[] input)
  13. {
  14. SymmetricAlgorithm aes = Rijndael.Create();
  15. aes.IV = _iv;
  16. aes.Key = _key;
  17. using (MemoryStream ms = new MemoryStream())
  18. {
  19. CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write);
  20. cs.Write(input, 0, input.Length);
  21. cs.FlushFinalBlock();
  22. return ms.ToArray();
  23. }
  24. }
  25. public static byte[] AESDecrypt(byte[] input)
  26. {
  27. SymmetricAlgorithm aes = Rijndael.Create();
  28. aes.IV = _iv;
  29. aes.Key = _key;
  30. using (MemoryStream ms = new MemoryStream())
  31. {
  32. CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write);
  33. cs.Write(input, 0, input.Length);
  34. cs.FlushFinalBlock();
  35. return ms.ToArray();
  36. }
  37. }
  38. }
  39. }