Encrypt and Decrypt Using Rijndael Key in C#
Last Updated :
23 Jul, 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
Introduction
C# TutorialC# (pronounced "C-sharp") is a modern, versatile, object-oriented programming language developed by Microsoft in 2000 that runs on the .NET Framework. Whether you're creating Windows applications, diving into Unity game development, or working on enterprise solutions, C# is one of the top choices fo
4 min read
Introduction to .NET FrameworkThe .NET Framework is a software development framework developed by Microsoft that provides a runtime environment and a set of libraries and tools for building and running applications on Windows operating systems. The .NET framework is primarily used on Windows, while .NET Core (which evolved into
6 min read
C# .NET Framework (Basic Architecture and Component Stack)C# (C-Sharp) is a modern, object-oriented programming language developed by Microsoft in 2000. It is a part of the .NET ecosystem and is widely used for building desktop, web, mobile, cloud, and enterprise applications. This is originally tied to the .NET Framework, C# has evolved to be the primary
6 min read
C# Hello WorldThe Hello World Program is the most basic program when we dive into a new programming language. This simply prints "Hello World!" on the console. In C#, a basic program consists of the following:A Namespace DeclarationClass Declaration & DefinitionClass Members(like variables, methods, etc.)Main
4 min read
Common Language Runtime (CLR) in C#The Common Language Runtime (CLR) is a component of the Microsoft .NET Framework that manages the execution of .NET applications. It is responsible for loading and executing the code written in various .NET programming languages, including C#, VB.NET, F#, and others.When a C# program is compiled, th
4 min read
Fundamentals
C# IdentifiersIn programming languages, identifiers are used for identification purposes. Or in other words, identifiers are the user-defined name of the program components. In C#, an identifier can be a class name, method name, variable name, or label. Example: public class GFG { static public void Main () { int
2 min read
C# Data TypesData types specify the type of data that a valid C# variable can hold. C# is a strongly typed programming language because in C# each type of data (such as integer, character, float, and so forth) is predefined as part of the programming language and all constants or variables defined for a given pr
7 min read
C# VariablesIn C#, variables are containers used to store data values during program execution. So basically, a Variable is a placeholder of the information which can be changed at runtime. And variables allows to Retrieve and Manipulate the stored information. In Brief Defination: When a user enters a new valu
4 min read
C# LiteralsIn C#, a literal is a fixed value used in a program. These values are directly written into the code and can be used by variables. A literal can be an integer, floating-point number, string, character, boolean, or even null. Example:// Here 100 is a constant/literal.int x = 100; Types of Literals in
5 min read
C# OperatorsIn C#, Operators are special types of symbols which perform operations on variables or values. It is a fundamental part of language which plays an important role in performing different mathematical operations. It takes one or more operands and performs operations to produce a result.Types of Operat
7 min read
C# KeywordsKeywords or Reserved words are the words in a language that are used for some internal process or represent some predefined actions. These words are therefore not allowed to be used as variable names or objects. Doing this will result in a compile-time error.Example:C#// C# Program to illustrate the
5 min read
Control Statements
C# Decision Making (if, if-else, if-else-if ladder, nested if, switch, nested switch)Decision Making in programming is similar to decision making in real life. In programming too, a certain block of code needs to be executed when some condition is fulfilled. A programming language uses control statements to control the flow of execution of program based on certain conditions. These
5 min read
C# Switch StatementIn C#, Switch statement is a multiway branch statement. It provides an efficient way to transfer the execution to different parts of a code based on the value of the expression. The switch expression is of integer type such as int, char, byte, or short, or of an enumeration type, or of string type.
4 min read
C# LoopsLooping in a programming language is a way to execute a statement or a set of statements multiple times, depending on the result of the condition to be evaluated to execute statements. The result condition should be true to execute statements within loops.Types of Loops in C#Loops are mainly divided
4 min read
C# Jump Statements (Break, Continue, Goto, Return and Throw)In C#, Jump statements are used to transfer control from one point to another point in the program due to some specified code while executing the program. In, this article, we will learn to different jump statements available to work in C#.Types of Jump StatementsThere are mainly five keywords in th
4 min read
OOP Concepts
Methods
Arrays
C# ArraysAn array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. The data types of the elements may be any valid data type like char, int, float, etc. and the elements are stored in a contiguous location. Length of the array spe
8 min read
C# Jagged ArraysA jagged array is an array of arrays, where each element in the main array can have a different length. In simpler terms, a jagged array is an array whose elements are themselves arrays. These inner arrays can have different lengths. Can also be mixed with multidimensional arrays. The number of rows
4 min read
C# Array ClassArray class in C# is part of the System namespace and provides methods for creating, searching, and sorting arrays. The Array class is not part of the System.Collections namespace, but it is still considered as a collection because it is based on the IList interface. The Array class is the base clas
7 min read
How to Sort an Array in C# | Array.Sort() Method Set - 1Array.Sort Method in C# is used to sort elements in a one-dimensional array. There are 17 methods in the overload list of this method as follows:Sort<T>(T[]) MethodSort<T>(T[], IComparer<T>) MethodSort<T>(T[], Int32, Int32) MethodSort<T>(T[], Comparison<T>) Method
8 min read
How to find the rank of an array in C#Array.Rank Property is used to get the rank of the Array. Rank is the number of dimensions of an array. For example, 1-D array returns 1, a 2-D array returns 2, and so on. Syntax: public int Rank { get; } Property Value: It returns the rank (number of dimensions) of the Array of type System.Int32. B
2 min read
ArrayList
String
Tuple
Indexers