Encrypt and Decrypt Using Rijndael Key in C#
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#.
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 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.
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.
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
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.
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.
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#.