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.
- using System;
- using System.Security.Cryptography;
- using System.Text;
- namespace DataEncrypterDecrypter
- {
- public class CryptoEngine
- {
- public static string Encrypt(string input, string key)
- {
- byte[] inputArray = UTF8Encoding.UTF8.GetBytes(input);
- TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
- tripleDES.Key = UTF8Encoding.UTF8.GetBytes(key);
- tripleDES.Mode = CipherMode.ECB;
- tripleDES.Padding = PaddingMode.PKCS7;
- ICryptoTransform cTransform = tripleDES.CreateEncryptor();
- byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length);
- tripleDES.Clear();
- return Convert.ToBase64String(resultArray, 0, resultArray.Length);
- }
- public static string Decrypt(string input, string key)
- {
- byte[] inputArray = Convert.FromBase64String(input);
- TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
- tripleDES.Key = UTF8Encoding.UTF8.GetBytes(key);
- tripleDES.Mode = CipherMode.ECB;
- tripleDES.Padding = PaddingMode.PKCS7;
- ICryptoTransform cTransform = tripleDES.CreateDecryptor();
- byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length);
- tripleDES.Clear();
- return UTF8Encoding.UTF8.GetString(resultArray);
- }
- }
- }
Here is how I used for demo purpose.
- private void encryptbtn_Click(object sender, RoutedEventArgs e)
- {
- if(plaintext.Text!= string.Empty)
- {
- //Here key is of 128 bit
- //Key should be either of 128 bit or of 192 bit
- Ciphertext.Text = CryptoEngine.Encrypt(plaintext.Text, "sblw-3hn8-sqoy19");
- }
- }
- private void decryptbtn_Click(object sender, RoutedEventArgs e)
- {
- if(Ciphertext.Text != string.Empty)
- {
- //Key shpuld be same for encryption and decryption
- decryptedtext.Text = CryptoEngine.Decrypt(Ciphertext.Text, "sblw-3hn8-sqoy19");
- }
- }


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

Fransiskus SenPosted May 3, 2020, 4:19 AM
Very good encryption
hadiyel jaydevPosted Jan 1, 2020, 4:28 AM
Hello Sir, I have done encryption and decryption on but I have to decrypt password at run time. I have tried many times some different questions but have not a success. Please, you have any idea so please give me suggestions.
Boris BorisPosted Dec 6, 2019, 9:30 AM
Is it possible to periodically amend the key, please? Like Master Key in SQL Server?
Siddhant JaiswalPosted Jul 23, 2018, 7:38 AM
What should be the maximum key size for encryption?
Ankita RawatPosted Feb 8, 2018, 7:47 AM
Nice and helpful articale
Upendra Pratap ShahiPosted Dec 19, 2017, 8:42 AM
Good one.......................................
Thaveesha KannangaraPosted Dec 19, 2017, 12:13 AM
Interesting Good One.. (Y)
Tony DongPosted Dec 12, 2017, 12:00 PM
TripleDES is old way, need to replaced by AES
Asfend YarPosted Jun 30, 2016, 7:15 AM
What if I ask you how to encrypt a complete file not a string ?
Harshad PansuriyaPosted Sep 11, 2015, 6:22 AM
Nice Share
Raju GuptaPosted Sep 8, 2015, 5:02 AM
good article
Santhakumar MunuswamyPosted Sep 5, 2015, 4:19 AM
Thanks for nice share
Sumit JoshiPosted Sep 4, 2015, 11:59 AM
Thanks For Sharing :)
Shakti SaxenaPosted Sep 4, 2015, 9:34 AM
Good explaination :) all the best
Pankaj Kumar ChoudharyPosted Sep 4, 2015, 6:56 AM
Nice Explain Janak and Thanks to become a part of this community ............
Ankit BansalPosted Sep 4, 2015, 5:04 AM
thanks for sharing...
Upendra Pratap ShahiPosted Sep 4, 2015, 4:42 AM
nice share..
Amol JadhaoPosted Sep 4, 2015, 3:48 AM
Thanks for Sharing..
Karthikeyan KPosted Sep 4, 2015, 2:21 AM
Good one sir...Thanks for sharing
Nihal AhmedPosted Sep 4, 2015, 1:18 AM
Nice one.
Rajeesh MenothPosted Sep 4, 2015, 12:49 AM
Good One..Thanks for sharing..Welcome to c# community
Shridhar SharmaPosted Sep 3, 2015, 2:57 PM
Nice start Janak Shrestha Bro. Welcome to the community.
Mohammed IbrahimPosted Sep 3, 2015, 2:06 PM
nice
RakeshPosted Sep 3, 2015, 1:30 PM
Good ones
Ravi MandalPosted Sep 3, 2015, 12:48 PM
Welcome to the Community bro ;)
Manas MohapatraPosted Sep 3, 2015, 12:44 PM
Informative one..