C# - Encrypt / Decrypt String Using Rijndael Key Algorithm

Learn, how to encrypt and decrypt a string using Rijndael key algorithm in C#?
Submitted by Nidhi, on October 13, 2020 [Last updated : March 21, 2023]

Here we will read a string and then encrypt/decrypt the input string using Rijdael key also.

C# program to encrypt and decrypt a string using Rijndael key algorithm

The source code to encrypt and decrypt a string using the Rijndael key algorithm is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to Encrypt/Decrypt string using Rijndael Key

using System;
using System.IO;
using System.Security.Cryptography;

class Crypto
{
    static byte[] EncryptData(string plainText, byte[] Key, byte[] IV)
    {
        byte[] encArray;
        RijndaelManaged algo = new RijndaelManaged();

        algo.Key = Key;
        algo.IV = IV;
        ICryptoTransform encryptor = algo.CreateEncryptor(algo.Key, algo.IV);
        MemoryStream msEncrypt = new MemoryStream();
        CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);

        using (StreamWriter Encrypt = new StreamWriter(csEncrypt))
        {
            Encrypt.Write(plainText);
        }
        encArray = msEncrypt.ToArray();

        return encArray;
    }
    static string DecryptData(byte[] cipherText, byte[] Key, byte[] IV)
    {

        string plaintext = null;
        RijndaelManaged algo = new RijndaelManaged();

        algo.Key = Key;
        algo.IV = IV;
        ICryptoTransform decryptor = algo.CreateDecryptor(algo.Key, algo.IV);
        MemoryStream msDecrypt = new MemoryStream(cipherText);

        CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);

        using (StreamReader Decrypt = new StreamReader(csDecrypt))
        {
            plaintext = Decrypt.ReadToEnd();
        }
        return plaintext;
    }
    public static void Main()
    {
        byte[] encData;
        string dcrptData;
        string str = "www.includehelp.com";
        RijndaelManaged enc = new RijndaelManaged();

        enc.GenerateKey();
        enc.GenerateIV();

        Console.WriteLine("Original String: ", str);

        encData = EncryptData(str, enc.Key, enc.IV);

        Console.WriteLine("Encrypted bytes: ");
        for (int i = 0; i < encData.Length; i++)
            Console.Write(encData[i]);
        dcrptData = DecryptData(encData, enc.Key, enc.IV);

        Console.WriteLine("\nDecrypted string: " + dcrptData);
    }
    
}

Output

Original String:
Encrypted bytes:
17013021097228170897011924915391321721782402233627184165199188188188391251261269
2208
Decrypted string: www.includehelp.com
Press any key to continue . . .

Explanation

Here, we created a class Crypto that contains three methods EncryptData(), DecryptData(), and Main() method.

The EncryptData() method is used to encrypt string into a byte array using CreateEncryptor() method of RijndaelManaged class and return the byte array to the calling method.

The DecryptData() method is used to decrypt the byte array into the string using CreateDecryptor() method of the RijndaelManaged class and then return the string to the calling method.

In the Main() method, we created a string str then encrypt and decrypt a string using EncryptData() and DcryptData() methods, then print data on the console screen.

C# Basic Programs »


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.