• Source
    1. using System;
    2. using System.Security.Cryptography;
    3. using System.Text;
    4.  
    5. public class Test
    6. {
    7. public static void Main()
    8. {
    9. string text = "Rvach.blogspot.com";
    10. byte[] plainData, cipher, key, IV;
    11. SymmetricAlgorithm cryptoAlgoritm;
    12. ICryptoTransform cryptor;
    13.  
    14. #region Encryptor
    15. plainData = Encoding.ASCII.GetBytes(text);
    16.  
    17. // This static method creates a new cryptographic using the default
    18. // algorithm, which in .NET 4.5 is RijndaelManaged.
    19. cryptoAlgoritm = SymmetricAlgorithm.Create();
    20. cryptor = cryptoAlgoritm.CreateEncryptor(cryptoAlgoritm.Key, cryptoAlgoritm.IV);
    21. cipher = cryptor.TransformFinalBlock(plainData, 0, plainData.Length);
    22.  
    23. key = cryptoAlgoritm.Key;
    24. IV = cryptoAlgoritm.IV;
    25.  
    26. cryptoAlgoritm.Clear();
    27.  
    28. Console.WriteLine("Cipher: "+ ASCIIEncoding.ASCII.GetString(cipher));
    29. #endregion
    30.  
    31. #region Decryptor
    32.  
    33. cryptoAlgoritm = SymmetricAlgorithm.Create();
    34. cryptor = cryptoAlgoritm.CreateDecryptor(key, IV);
    35. plainData = cryptor.TransformFinalBlock(cipher, 0, cipher.Length);
    36.  
    37. cryptoAlgoritm.Clear();
    38.  
    39. Console.WriteLine("Plain: "+ ASCIIEncoding.ASCII.GetString(plainData));
    40. #endregion
    41.  
    42. Console.ReadKey();
    43. }
    44. }