fork download
  1. using System;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. namespace AESExample
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. Aes aes = Aes.Create();
  11. aes.KeySize = 256;
  12. aes.Mode = CipherMode.CBC;
  13. aes.GenerateKey();
  14. aes.GenerateIV();
  15. string plaintext = "Hello, world!";
  16. byte[] ciphertext;
  17. using (ICryptoTransform encryptor = aes.CreateEncryptor())
  18. {
  19. byte[] plaintextBytes = Encoding.UTF8.GetBytes(plaintext);
  20. ciphertext = encryptor.TransformFinalBlock(plaintextBytes, 0, plaintextBytes.Length);
  21. }
  22. string decryptedText;
  23. using (ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV))
  24. {
  25. byte[] decryptedBytes = decryptor.TransformFinalBlock(ciphertext, 0, ciphertext.Length);
  26. decryptedText = Encoding.UTF8.GetString(decryptedBytes);
  27. }
  28. Console.WriteLine($"Plaintext: {plaintext}");
  29. Console.WriteLine($"Ciphertext: {Convert.ToBase64String(ciphertext)}");
  30. Console.WriteLine($"Decrypted text: {decryptedText}");
  31. }
  32. }
  33. }
Success #stdin #stdout 0.07s 33068KB
stdin
Standard input is empty
stdout
Plaintext: Hello, world!
Ciphertext: s9Pev/wGcgKhDklRXb5upw==
Decrypted text: Hello, world!