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 w3wiki 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 “w3wiki 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 = "w3wiki 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 

  • MemoryStream
  • CryptoStream

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 = "w3wiki 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 = "w3wiki 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 : 

 

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#. 

Similar Reads

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...

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....

Decryption of a String:

...