RdAES.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. namespace Ragdoll
  6. {
  7. public class EnUtil
  8. {
  9. public static string En(string source)
  10. {
  11. const string key = "M6MoLC88bDjDE0qtoi4hlXNHfZ2Aa6bS";
  12. const string IV = "UjLbdUwyqdhp5pGf";
  13. return AesEncryptedString(source, key, IV);
  14. }
  15. public static string AesEncryptedString(string encryptStr, string key, string vector)
  16. {
  17. byte[] aesBytes = Encoding.UTF8.GetBytes(encryptStr);
  18. byte[] aesKey = new byte[32];
  19. Array.Copy(Encoding.UTF8.GetBytes(key), aesKey, aesKey.Length);
  20. byte[] aesVector = new byte[16];
  21. Array.Copy(Encoding.UTF8.GetBytes(vector), aesVector, aesVector.Length);
  22. Rijndael Aes = Rijndael.Create();
  23. //或者采用下方生成Aes
  24. //RijndaelManaged Aes = new();
  25. // 开辟一块内存流
  26. MemoryStream memoryStream = new MemoryStream();
  27. // 把内存流对象包装成加密流对象
  28. CryptoStream cryptoStream = new CryptoStream(memoryStream, Aes.CreateEncryptor(aesKey, aesVector),
  29. CryptoStreamMode.Write);
  30. // 明文数据写入加密流
  31. cryptoStream.Write(aesBytes, 0, aesBytes.Length);
  32. cryptoStream.FlushFinalBlock();
  33. string result = Convert.ToBase64String(memoryStream.ToArray());
  34. return result;
  35. }
  36. public static string Sha256Encrypt(string palinData, string salt = "cxhy")
  37. {
  38. if (string.IsNullOrWhiteSpace(palinData)) return null;
  39. using (SHA256 sha256 = new SHA256CryptoServiceProvider())
  40. {
  41. byte[] bytes = Encoding.UTF8.GetBytes(palinData + salt);
  42. byte[] sha256Bytes = sha256.ComputeHash(bytes);
  43. return Convert.ToBase64String(sha256Bytes);
  44. }
  45. }
  46. public static string AesAndSha256(string content)
  47. {
  48. return Sha256Encrypt(En(content));
  49. }
  50. }
  51. }