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, 12:19 AM EDT
Very good encryption
hadiyel jaydevPosted Dec 31, 2019, 11:28 PM EST
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, 4:30 AM EST
Is it possible to periodically amend the key, please? Like Master Key in SQL Server?
Siddhant JaiswalPosted Jul 23, 2018, 3:38 AM EDT
What should be the maximum key size for encryption?
Ankita RawatPosted Feb 8, 2018, 2:47 AM EST
Nice and helpful articale
Upendra Pratap ShahiPosted Dec 19, 2017, 3:42 AM EST
Good one.......................................
Thaveesha KannangaraPosted Dec 18, 2017, 7:13 PM EST
Interesting Good One.. (Y)
Tony DongPosted Dec 12, 2017, 7:00 AM EST
TripleDES is old way, need to replaced by AES
Asfend YarPosted Jun 30, 2016, 3:15 AM EDT
What if I ask you how to encrypt a complete file not a string ?
Harshad PansuriyaPosted Sep 11, 2015, 2:22 AM EDT
Nice Share
Raju GuptaPosted Sep 8, 2015, 1:02 AM EDT
good article
Santhakumar MunuswamyPosted Sep 5, 2015, 12:19 AM EDT
Thanks for nice share
Sumit JoshiPosted Sep 4, 2015, 7:59 AM EDT
Thanks For Sharing :)
Shakti SaxenaPosted Sep 4, 2015, 5:34 AM EDT
Good explaination :) all the best
Pankaj Kumar ChoudharyTOP 100Posted Sep 4, 2015, 2:56 AM EDT
Nice Explain Janak and Thanks to become a part of this community ............
Ankit BansalPosted Sep 4, 2015, 1:04 AM EDT
thanks for sharing...
Upendra Pratap ShahiPosted Sep 4, 2015, 12:42 AM EDT
nice share..
Amol JadhaoPosted Sep 3, 2015, 11:48 PM EDT
Thanks for Sharing..
Karthikeyan KPosted Sep 3, 2015, 10:21 PM EDT
Good one sir...Thanks for sharing
Nihal AhmedPosted Sep 3, 2015, 9:18 PM EDT
Nice one.
Rajeesh MenothPosted Sep 3, 2015, 8:49 PM EDT
Good One..Thanks for sharing..Welcome to c# community
Shridhar SharmaPosted Sep 3, 2015, 10:57 AM EDT
Nice start Janak Shrestha Bro. Welcome to the community.
Mohammed IbrahimPosted Sep 3, 2015, 10:06 AM EDT
nice
RakeshPosted Sep 3, 2015, 9:30 AM EDT
Good ones
Ravi MandalPosted Sep 3, 2015, 8:48 AM EDT
Welcome to the Community bro ;)
Manas MohapatraPosted Sep 3, 2015, 8:44 AM EDT
Informative one..