EnryptionClass.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 ApplicationAvalonia.Utilities
  9. {
  10. public static class EncryptionClass
  11. {
  12. public static byte[] Encrypt(string simpleText, byte[] key, byte[] iv)
  13. {
  14. byte[] cipheredText;
  15. using (Aes aes = Aes.Create())
  16. {
  17. ICryptoTransform encryptor = aes.CreateEncryptor(key, iv);
  18. using (MemoryStream memoryStream = new MemoryStream())
  19. {
  20. using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
  21. {
  22. using (StreamWriter streamWriter = new StreamWriter(cryptoStream))
  23. {
  24. streamWriter.Write(simpleText);
  25. }
  26. cipheredText = memoryStream.ToArray();
  27. }
  28. }
  29. }
  30. return cipheredText;
  31. }
  32. public static string Decrypt(byte[] cipheretext, byte[] key, byte[] iv)
  33. {
  34. string simpleText = String.Empty;
  35. using (Aes aes = Aes.Create())
  36. {
  37. ICryptoTransform decryptor = aes.CreateDecryptor(key, iv);
  38. using (MemoryStream memoryStream = new MemoryStream(cipheretext))
  39. {
  40. using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
  41. {
  42. using (StreamReader streamReader = new StreamReader(cryptoStream))
  43. {
  44. simpleText = streamReader.ReadToEnd();
  45. }
  46. }
  47. }
  48. }
  49. return simpleText;
  50. }
  51. }
  52. }