Encryption And Decryption in C#

Encryption And Decryption
  1. using System;  
  2. using System.IO;  
  3. using System.Security.Cryptography;  
  4. using System.Text;  
  5.   
  6. namespace EncryptionDecryptionUsingSymmetricKey  
  7. {  
  8.     public class AesOperation  
  9.     {  
  10.         public static string EncryptString(string key, string plainText)  
  11.         {  
  12.             byte[] iv = new byte[16];  
  13.             byte[] array;  
  14.   
  15.             using (Aes aes = Aes.Create())  
  16.             {  
  17.                 aes.Key = Encoding.UTF8.GetBytes(key);  
  18.                 aes.IV = iv;  
  19.   
  20.                 ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);  
  21.   
  22.                 using (MemoryStream memoryStream = new MemoryStream())  
  23.                 {  
  24.                     using (CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, encryptor, CryptoStreamMode.Write))  
  25.                     {  
  26.                         using (StreamWriter streamWriter = new StreamWriter((Stream)cryptoStream))  
  27.                         {  
  28.                             streamWriter.Write(plainText);  
  29.                         }  
  30.   
  31.                         array = memoryStream.ToArray();  
  32.                     }  
  33.                 }  
  34.             }  
  35.   
  36.             return Convert.ToBase64String(array);  
  37.         }  
  38.   
  39.         public static string DecryptString(string key, string cipherText)  
  40.         {  
  41.             byte[] iv = new byte[16];  
  42.             byte[] buffer = Convert.FromBase64String(cipherText);  
  43.   
  44.             using (Aes aes = Aes.Create())  
  45.             {  
  46.                 aes.Key = Encoding.UTF8.GetBytes(key);  
  47.                 aes.IV = iv;  
  48.                 ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);  
  49.   
  50.                 using (MemoryStream memoryStream = new MemoryStream(buffer))  
  51.                 {  
  52.                     using (CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, decryptor, CryptoStreamMode.Read))  
  53.                     {  
  54.                         using (StreamReader streamReader = new StreamReader((Stream)cryptoStream))  
  55.                         {  
  56.                             return streamReader.ReadToEnd();  
  57.                         }  
  58.                     }  
  59.                 }  
  60.             }  
  61.         }  
  62.     }  
  63. }  

  1. using System;  
  2.   
  3. namespace EncryptionDecryptionUsingSymmetricKey  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             var key = “b14ca5898a4e4133bbce2ea2315a1916”;  
  10.   
  11.             //Console.WriteLine(“Please enter a secret key for the symmetric algorithm.”);  
  12.             //var key = Console.ReadLine();  
  13.   
  14.             Console.WriteLine(“Please enter a string for encryption”);  
  15.             var str = Console.ReadLine();  
  16.             var encryptedString = AesOperation.EncryptString(key, str);  
  17.             Console.WriteLine($”encrypted string = {encryptedString}”);  
  18.   
  19.             var decryptedString = AesOperation.DecryptString(key, encryptedString);  
  20.             Console.WriteLine($”decrypted string = {decryptedString}”);  
  21.   
  22.             Console.ReadKey();  
  23.         }  
  24.     }  
  25. }  

Please Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *