Data Encryption And Decryption in C#

Sometimes somewhere we have to perform encryption and decryption of data. So today I will show how to encrypt and decrypt data in our application. For this demo I used WPF application. Doing encryption and decryption is easy but we have to remember/know few things.

This example shows how you can use C# to encrypt and decrypt strings using a salt key to protect the data.

This type of encryption is called symmetric-key encryption that means the string can only be decrypted if the other party has the correct key (which is used for encryption).

So here is the code for encryption and decryption.

  1. using System;  
  2. using System.Security.Cryptography;  
  3. using System.Text;  
  4.    
  5. namespace DataEncrypterDecrypter  
  6. {  
  7.     public class CryptoEngine  
  8.     {  
  9.         public static string Encrypt(string input, string key)  
  10.         {  
  11.             byte[] inputArray = UTF8Encoding.UTF8.GetBytes(input);  
  12.             TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();  
  13.             tripleDES.Key = UTF8Encoding.UTF8.GetBytes(key);  
  14.             tripleDES.Mode = CipherMode.ECB;  
  15.             tripleDES.Padding = PaddingMode.PKCS7;  
  16.             ICryptoTransform cTransform = tripleDES.CreateEncryptor();  
  17.             byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length);  
  18.             tripleDES.Clear();  
  19.             return Convert.ToBase64String(resultArray, 0, resultArray.Length);  
  20.         }  
  21.         public static string Decrypt(string input, string key)  
  22.         {  
  23.             byte[] inputArray = Convert.FromBase64String(input);  
  24.             TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();  
  25.             tripleDES.Key = UTF8Encoding.UTF8.GetBytes(key);  
  26.             tripleDES.Mode = CipherMode.ECB;  
  27.             tripleDES.Padding = PaddingMode.PKCS7;  
  28.             ICryptoTransform cTransform = tripleDES.CreateDecryptor();  
  29.             byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length);  
  30.             tripleDES.Clear();   
  31.             return UTF8Encoding.UTF8.GetString(resultArray);  
  32.         }  
  33.     }  
  34. }  
As you see in code, the CipherMode and PaddingMode must be the same for both the operations. There is multiple options for CipherMode and PaddingMode, you can read their differences and choose which one is the best for your need. In case of key that we pass as parameter for both the operations, it must be the same for both of them. This key is what your application use for encryption and decryption which you should keep secret else anyone can decrypt your data with that key and it must be either 128 bit or 192 bit (not even in between). If you don’t know that your supplied key is either 128/192 bit or not you can use this application for bit calculation.

Here is how I used for demo purpose.
  1. private void encryptbtn_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     if(plaintext.Text!= string.Empty)  
  4.     {  
  5.         //Here key is of 128 bit  
  6.         //Key should be either of 128 bit or of 192 bit  
  7.         Ciphertext.Text = CryptoEngine.Encrypt(plaintext.Text, "sblw-3hn8-sqoy19");  
  8.     }  
  9. }  
  10.   
  11. private void decryptbtn_Click(object sender, RoutedEventArgs e)  
  12. {  
  13.     if(Ciphertext.Text != string.Empty)  
  14.     {  
  15.         //Key shpuld be same for encryption and decryption  
  16.         decryptedtext.Text = CryptoEngine.Decrypt(Ciphertext.Text, "sblw-3hn8-sqoy19");  
  17.     }  
  18. }  
Crypto Engine

decrypted text

That’s it. Now you can use these methods/operations as per your need. You can download the sample code from here.

 


Similar Articles