Encrypt and Decrypt Using Rijndael Key in C#
Last Updated :
26 Apr, 2025
To keep data secure and protected it is necessary to keep the data encrypted. As we know that in C# and in other languages too there are many ways for encrypting data. The Data Encryption Standard method used for encryption was not promising good security that led to the invention of a highly secure tool called Rijndael Key by Vincent Rijmen and Joan Daemon. In this article, we will learn about the Rijndael key and perform step-by-step Encryption and Decryption of certain data by using Rijndael Key in C#.
Block Cipher:
A block cipher is a method of encrypting data in blocks for producing a cipher text using a cryptographic key and an algorithm. block ciphers are more secure and reliable than Standard Data Encryption (DES).
Rijndael Key:
Rijndael is based on the block cipher method which uses a symmetric key encryption technique. It works with the help of invertible and discrete layers
- Linear Mix Transform
- Non - Linear Transform
- Key Addition Transform
As for C#, the Rijndael key supports key lengths of 128, 192, and 256 bits and also supports blocks of 128 (by default), 192, and 256 bits. Rijndael key is very much similar to AES(Advance Encryption Standard).
Implementation of Encryption of a String:
Step 1: The first step would be to create a C# file in the IDE of your choice or you can just use the GeeksForGeeks IDE. Name the Class "GFGEncryption" to keep things simple and aligned with the tutorial.
Step 2:Now make a new method named encodeString( ) which takes no parameters and returns a string.
Step 3: Now inside the blocks of encodeString method, we will write the actual code for encoding our string. we will use the string "GeeksForGeeks Text". We will encode this string. Inside the method, we have to create multiple variables.
Note :
The public and private key both must have at least 8 characters.
Don't forget to add import statements.
Example:
C#
using System.IO;
using System.Security.Cryptography;
using System.Text;
public class GFGEncryption{
static public void Main (){
}
public static string encodeString (){
string data = "GeeksForGeeks Text";
string answer = "";
string publicKey = "GEEK1234";
string privateKey = "PKEY4321";
byte[] privatekeyBytes = Encoding.UTF8.GetBytes(privateKey);
byte[] publicKeyBytes = Encoding.UTF8.GetBytes(publicKey);
byte[] inputByteArray= System.Text.Encoding.UTF8.GetBytes(data);
return answer;
}
}
Step 4: Once we have created all the required variables we can now perform the actual encoding operation by using the class called "DESCryptoServiceProvider". Now inside the block of this class, we will create two new objects of the type
We will use the Write method from CryptoStream class and pass the input byte array and its length into it resulting in an encoded array. Your code must look as below.
Example:
C#
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System;
public class GFGEncryption{
static public void Main (){
}
public static string encodeString (){
string data = "GeeksForGeeks Text";
string answer = "";
string publicKey = "GEEK1234";
string privateKey = "PKEY4321";
byte[] privateKeyBytes ={};
privateKeyBytes = Encoding.UTF8.GetBytes(privateKey);
byte[] publicKeyBytes = {};
publicKeyBytes = Encoding.UTF8.GetBytes(publicKey);
byte[] inputByteArray= System.Text.Encoding.UTF8.GetBytes(data);
using (DESCryptoServiceProvider provider = new DESCryptoServiceProvider())
{
var memoryStream = new MemoryStream();
var cryptoStream = new CryptoStream(memoryStream,
provider.CreateEncryptor(publicKeyBytes, privateKeyBytes),
CryptoStreamMode.Write);
cryptoStream.Write(inputByteArray, 0, inputByteArray.Length);
cryptoStream.FlushFinalBlock();
answer = Convert.ToBase64String(memoryStream.ToArray());
}
return answer;
}
}
Step 5: Finally, we have successfully implemented the encodeString( ) method and we are going to use it in our main class. You should use the method as shown below
Example:
C#
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System;
public class GFGEncryption{
static public void Main (){
string encryptedString = encodeString();
Console.Write("Encoded String is: " +encryptedString);
}
public static string encodeString(){
string data = "GeeksForGeeks Text";
string answer = "";
string publicKey = "GEEK1234";
string privateKey = "PKEY4321";
byte[] privateKeyBytes ={};
privateKeyBytes = Encoding.UTF8.GetBytes(privateKey);
byte[] publicKeyBytes = {};
publicKeyBytes = Encoding.UTF8.GetBytes(publicKey);
byte[] inputByteArray= System.Text.Encoding.UTF8.GetBytes(data);
using (DESCryptoServiceProvider provider = new DESCryptoServiceProvider())
{
var memoryStream = new MemoryStream();
var cryptoStream = new CryptoStream(memoryStream,
provider.CreateEncryptor(publicKeyBytes, privateKeyBytes),
CryptoStreamMode.Write);
cryptoStream.Write(inputByteArray, 0, inputByteArray.Length);
cryptoStream.FlushFinalBlock();
answer = Convert.ToBase64String(memoryStream.ToArray());
}
return answer;
}
}
Output :
Decryption of a String:
Step 1: Similarly, as we created an encoded string method, we will create a decodeString( ) method which decodes the given encrypted string and returns its true value. create the decodeString( ) method just like shown below.
Example:
C#
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System;
public class GFGEncryption{
static public void Main (){
string encryptedString = encodeString();
Console.Write("Encoded String is: " +encryptedString);
}
public static string encodeString(){
string data = "GeeksForGeeks Text";
string answer = "";
string publicKey = "GEEK1234";
string privateKey = "PKEY4321";
byte[] privateKeyBytes ={};
privateKeyBytes = Encoding.UTF8.GetBytes(privateKey);
byte[] publicKeyBytes = {};
publicKeyBytes = Encoding.UTF8.GetBytes(publicKey);
byte[] inputByteArray= System.Text.Encoding.UTF8.GetBytes(data);
using (DESCryptoServiceProvider provider = new DESCryptoServiceProvider())
{
var memoryStream = new MemoryStream();
var cryptoStream = new CryptoStream(memoryStream,
provider.CreateEncryptor(publicKeyBytes, privateKeyBytes),
CryptoStreamMode.Write);
cryptoStream.Write(inputByteArray, 0, inputByteArray.Length);
cryptoStream.FlushFinalBlock();
answer = Convert.ToBase64String(memoryStream.ToArray());
}
return answer;
}
public static string decodeString(String str) {
string answer = "";
return answer;
}
}
Step 2: Now inside the decode method add all the variables just like we created in the encodeString( ) method. This time instead of creating a separate variable "data" we will directly use the data variable which is coming from methods parameters. Do it as demonstrated.
Example:
C#
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System;
public class GFGEncryption{
static public void Main (){
string encryptedString = encodeString();
Console.Write("Encoded String is: " +encryptedString);
}
public static string encodeString(){
string data = "GeeksForGeeks Text";
string answer = "";
string publicKey = "GEEK1234";
string privateKey = "PKEY4321";
byte[] privateKeyBytes ={};
privateKeyBytes = Encoding.UTF8.GetBytes(privateKey);
byte[] publicKeyBytes = {};
publicKeyBytes = Encoding.UTF8.GetBytes(publicKey);
byte[] inputByteArray= System.Text.Encoding.UTF8.GetBytes(data);
using (DESCryptoServiceProvider provider = new DESCryptoServiceProvider())
{
var memoryStream = new MemoryStream();
var cryptoStream = new CryptoStream(memoryStream,
provider.CreateEncryptor(publicKeyBytes, privateKeyBytes),
CryptoStreamMode.Write);
cryptoStream.Write(inputByteArray, 0, inputByteArray.Length);
cryptoStream.FlushFinalBlock();
answer = Convert.ToBase64String(memoryStream.ToArray());
}
return answer;
}
public static string decodeString(String data) {
string answer = "";
string publicKey = "GEEK1234";
string privateKey = "PKEY4321";
byte[] privateKeyBytes ={};
privateKeyBytes = Encoding.UTF8.GetBytes(privateKey);
byte[] publicKeyBytes = {};
publicKeyBytes = Encoding.UTF8.GetBytes(publicKey);
byte[] inputByteArray= new byte[data.Replace(" ", "+").Length];
using (DESCryptoServiceProvider provider = new DESCryptoServiceProvider())
{
var memoryStream = new MemoryStream();
var cryptoStream = new CryptoStream(memoryStream,
provider.CreateEncryptor(publicKeyBytes, privateKeyBytes),
CryptoStreamMode.Write);
cryptoStream.Write(inputByteArray, 0, inputByteArray.Length);
cryptoStream.FlushFinalBlock();
answer = Encoding.UTF8.GetString(memoryStream.ToArray());
}
return answer;
}
}
Step 3: As we have successfully implemented the method for decrypting the encoded string we will use the method in the main function and see if the decoded value is true or not. Remember we used the string "GeeksForGeeks Text". If we this string as a result we can conclude that we have successfully performed encryption and decryption in C#.
Example:
C#
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System;
public class GFGEncryption{
static public void Main (){
string encryptedString = encodeString();
string decryptedString = decodeString(encryptedString);
Console.Write("Encoded String is: " +encryptedString);
Console.Write("\nDecoded String is: " +decryptedString);
}
public static string encodeString(){
string data = "GeeksForGeeks Text";
string answer = "";
string publicKey = "GEEK1234";
string privateKey = "PKEY4321";
byte[] privateKeyBytes ={};
privateKeyBytes = Encoding.UTF8.GetBytes(privateKey);
byte[] publicKeyBytes = {};
publicKeyBytes = Encoding.UTF8.GetBytes(publicKey);
byte[] inputByteArray= System.Text.Encoding.UTF8.GetBytes(data);
using (DESCryptoServiceProvider provider = new DESCryptoServiceProvider())
{
var memoryStream = new MemoryStream();
var cryptoStream = new CryptoStream(memoryStream,
provider.CreateEncryptor(publicKeyBytes, privateKeyBytes),
CryptoStreamMode.Write);
cryptoStream.Write(inputByteArray, 0, inputByteArray.Length);
cryptoStream.FlushFinalBlock();
answer = Convert.ToBase64String(memoryStream.ToArray());
}
return answer;
}
public static string decodeString(String data) {
string answer = "";
string publicKey = "GEEK1234";
string privateKey = "PKEY4321";
byte[] privateKeyBytes ={};
privateKeyBytes = Encoding.UTF8.GetBytes(privateKey);
byte[] publicKeyBytes = {};
publicKeyBytes = Encoding.UTF8.GetBytes(publicKey);
byte[] inputByteArray= new byte[data.Replace(" ", "+").Length];
inputByteArray = Convert.FromBase64String(data.Replace(" ", "+"));
using (DESCryptoServiceProvider provider = new DESCryptoServiceProvider())
{
var memoryStream = new MemoryStream();
var cryptoStream = new CryptoStream(memoryStream,
provider.CreateDecryptor(publicKeyBytes, privateKeyBytes),
CryptoStreamMode.Write);
cryptoStream.Write(inputByteArray, 0, inputByteArray.Length);
cryptoStream.FlushFinalBlock();
answer = Encoding.UTF8.GetString(memoryStream.ToArray());
}
return answer;
}
}
Output:
Similar Reads
Encrypt and Decrypt String File Using Java
In the field of cryptography, encryption is the process of turning plain text or information into ciphertext, or text that can only be deciphered by the intended recipient. A cipher is a term used to describe the encryption algorithm. It secures communication networks and aids in preventing illegal
3 min read
Encrypt and Decrypt Files using Python
Encryption is the act of encoding a message so that only the intended users can see it. We encrypt data because we don't want anyone to see or access it. We will use the cryptography library to encrypt a file. The cryptography library uses a symmetric algorithm to encrypt the file. In the symmetric
3 min read
Encrypt/Decrypt Files in Linux using Ccrypt
Ccrypt is a command line tool for encryption and decryption of data. Ccrypt is based on the Rijndael cipher, the same cipher used in the AES standard. On the other hand, in the AES standard, a 128-bit block size is used, whereas ccrypt uses a 256-bit block size. Ccrypt commonly uses the .cpt file ex
3 min read
How to Encrypt and Decrypt Strings in Python?
In this article, we will learn about Encryption, Decryption and implement them with Python. Encryption:Encryption is the process of encoding the data. i.e converting plain text into ciphertext. This conversion is done with a key called an encryption key.Decryption:Decryption is the process of decodi
5 min read
Encrypting and Decrypting the Files Using GnuPG in Linux
GnuPG is an encryption module that uses OpenPGP at its core. PGP stands for Pretty Good Privacy. It is an encryption program that provides authentication and cryptographic privacy for data communication. In the age where data is the new oil, the modern age thieves won't intrude in through doors, win
3 min read
How to Encrypt and Decrypt Text in Android Using Cryptography?
Cryptography is a technique of securing information and communications through the use of codes so that only those people for whom the information is intended can understand it and process it. Thus preventing unauthorized access to information. The prefix âcryptâ means âhiddenâ and suffix graphy mea
15+ min read
Blockchain - Encrypt & Decrypt Files With Password Using OpenSSL
Encryption is the process of encoding information or data in such a way that only authorized parties can access it. The process of encrypting information involves using a mathematical algorithm to transform the original information, known as plaintext, into a form that is unreadable to anyone who do
5 min read
Data Encryption and Security in R
Data encryption and security are crucial aspects of data management and analysis, especially when dealing with sensitive information. R is a powerful statistical programming language, that provides various packages and functions to ensure data security through encryption, secure storage, and secure
3 min read
Encrypting Files Using vim editor in Linux
Vim, the popular text editor comes with a built-in feature to encrypt files with a password. Vim uses algorithms like Blowfish to encrypt files. It is faster and convenient than some other utilities available to do so. To get started, all you need is a system with a full version of vim installed. So
3 min read
How to Setup Encrypted Filesystems and Swap Space Using âCryptsetupâ Tool in Linux
An LFCE is in charge of the design, implementation, and continuous maintenance of the system architecture and is qualified and experienced to install, administer, and troubleshoot network services in Linux systems. Hard disc encryption for Linux Introduction to The Linux Foundation Certification Pro
4 min read