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);
- }
- }
- }
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.
- private void encryptbtn_Click(object sender, RoutedEventArgs e)
- {
- if(plaintext.Text!= string.Empty)
- {
-
-
- Ciphertext.Text = CryptoEngine.Encrypt(plaintext.Text, "sblw-3hn8-sqoy19");
- }
- }
-
- private void decryptbtn_Click(object sender, RoutedEventArgs e)
- {
- if(Ciphertext.Text != string.Empty)
- {
-
- 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.