:::: MENU ::::

Encryption and Decryption in C#

Sometime and 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 data using a single salt key for every data.
This type of encryption is called symmetric-key encryption, which means that the string can only be decrypted if the other party has the correct key.

So here is code for encryption and decryption

As you see in code CipherMode and PaddingMode must be same for both of operation. There is multiple options for CipherMode and PaddingMode you can read their differences and choose which one is best for your need. In case of key that we pass as parameter for both of operation it must be the same for both operation. 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.

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

This type of cryptographic technique is not used any more to secure system but you can used for simple example purpose.

Happy Coding. 🙂