How to Encrypt and Decrypt a String in C#

By FoxLearn 11/20/2024 9:59:59 AM   13.76K
In C#, you can use TripleDES (Triple Data Encryption Standard) for encrypting and decrypting strings in a Windows Forms application.

The System.Security.Cryptography namespace provides the necessary classes to implement TripleDES encryption and decryption.

How to encrypt and decrypt a string using TripleDES in C#?

Open Visual Studio, then click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "EncryptDecrypt" and then click OK

Drag and drop the Label, Button, and Textbox controls from the Visual Toolbox onto your form designer, then design your form as shown below.

encrypt decrypt string in c#

Add code to handle your form

using System;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;

namespace EncryptDecrypt
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string hash = "f0xle@rn";//Create a hash key

        // This method will encrypt a plain text string using TripleDES.
        private void btnEncrypt_Click(object sender, EventArgs e)
        {
            // Input text to bytes
            byte[] data = UTF8Encoding.UTF8.GetBytes(txtValue.Text);
            using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
            {
                byte[] keys = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(hash));//Get hash key
                // Encrypt data using TripleDES
                using (TripleDESCryptoServiceProvider tripDes = new TripleDESCryptoServiceProvider() { Key = keys, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 })
                {
                    // Encrypt data
                    ICryptoTransform transform = tripDes.CreateEncryptor();
                    byte[] results = transform.TransformFinalBlock(data, 0, data.Length);
                    // Convert encrypted data to base64 string
                    txtEncrypt.Text = Convert.ToBase64String(results, 0, results.Length);
                }
            }
        }

        // This method will decrypt the encrypted string back to plain text.
        private void btnDecrypt_Click(object sender, EventArgs e)
        {
            // Convert the base64 encoded encrypted string to a byte array
            byte[] data = Convert.FromBase64String(txtEncrypt.Text);
            // Decrypt data using TripleDES
            using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
            {
                byte[] keys = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(hash));//Get hash key
                //Decrypt data by hash key
                using (TripleDESCryptoServiceProvider tripDes = new TripleDESCryptoServiceProvider() { Key = keys, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 })
                {
                    ICryptoTransform transform = tripDes.CreateDecryptor();
                    byte[] results = transform.TransformFinalBlock(data, 0, data.Length);
                    txtDecrypt.Text = UTF8Encoding.UTF8.GetString(results);
                }
            }
        }
    }
}

TripleDES (3DES) is a symmetric encryption algorithm that applies the DES (Data Encryption Standard) algorithm three times to each data block. It uses either two or three 56-bit keys, effectively creating a key length of 112 or 168 bits.

VIDEO TUTORIAL

Related