LogSplicingUtil.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. namespace ET
  6. {
  7. public static class LogSplicingUtil
  8. {
  9. /// <summary>
  10. /// AES加密算法
  11. /// </summary>
  12. /// <param name="plainText">明文字符串</param>
  13. /// <param name="strKey">密钥</param>
  14. /// <returns>返回加密后的密文字节数组</returns>
  15. public static string AESEncrypt(string plainText, string strKey)
  16. {
  17. //分组加密算法
  18. SymmetricAlgorithm des = Aes.Create();
  19. byte[] inputByteArray = Encoding.UTF8.GetBytes(plainText); //得到需要加密的字节数组
  20. //设置密钥及密钥向量
  21. byte[] key1 =
  22. {
  23. 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB,
  24. 0xCD, 0xEF
  25. };
  26. des.Key = Encoding.UTF8.GetBytes(strKey);
  27. des.IV = key1;
  28. MemoryStream ms = new MemoryStream();
  29. CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
  30. cs.Write(inputByteArray, 0, inputByteArray.Length);
  31. cs.FlushFinalBlock();
  32. byte[] cipherBytes = ms.ToArray(); //得到加密后的字节数组
  33. cs.Close();
  34. ms.Close();
  35. return Convert.ToBase64String(cipherBytes);
  36. }
  37. }
  38. }