Symmetric encryption

Symmetric encryption uses a single key and works on blocks of text. This method works quicker than others. While using this method, it is important to maintain the confidentiality of the secret key, and both the sender and receiver should use the same key, which is a disadvantage of this method.

Let's look at an example and understand how we can encrypt a message or block of text: 

  1. Here, we use the encrypt method, where we read a block of text from a file, encrypt it using a symmetric algorithm, and write the encrypted content to a different file.
  2. The encrypt method accepts an instance of SymmetricAlgorithm, which is used to create an instance of ICryptoTransform by passing a key and initial vector. The system allows you to generate your own key or use the one it generates.
  3. Then, we create a memory stream to store the buffer at runtime:
public static void EncryptSymmetric(SymmetricAlgorithm sem)
{
//Read content from file
string filecontent = File.ReadAllText("..\..\inputfile.txt");
//Create encryptor using key and vector
ICryptoTransform encryptor = sem.CreateEncryptor(sem.Key, sem.IV);
//create memory stream used at runtime to store data
using (MemoryStream outputstream = new MemoryStream())
{
//Create crypto stream in write mode
using (CryptoStream encStream = new CryptoStream(outputstream, encryptor, CryptoStreamMode.Write))
{
//use streamwrite
using (StreamWriter writer = new StreamWriter(encStream))
{
// Write the text in the stream writer
writer.Write(filecontent);
}
}
// Get the result as a byte array from the memory stream
byte[] encryptedDataFromMemory = outputstream.ToArray();
// Write the data to a file
File.WriteAllBytes("..\..\Outputfile.txt", encryptedDataFromMemory);
}
}

A cryptostream is created using the memory stream, ICryptoTransform, along with the write mode. A cryptostream is used to write to memory, which can then be converted into an array and written to an output file.

  1. Once you execute the encrypt method, you can now open the output file from the solution and check the results.
  2. Now, we will read the data from the output file and decrypt it to plain text:
public static string DecryptSymmetric(SymmetricAlgorithm sem)
{
string result = string.Empty;
//Create decryptor
ICryptoTransform decryptor = sem.CreateDecryptor(sem.Key, sem.IV);
//read file content
byte[] filecontent = File.ReadAllBytes("..\..\Outputfile.txt");
//read file content to memory stream
using (MemoryStream outputstream = new MemoryStream(filecontent))
{
//create decrypt stream
using (CryptoStream decryptStream = new CryptoStream(outputstream, decryptor, CryptoStreamMode.Read))
{
using (StreamReader reader = new StreamReader(decryptStream))
{
//read content of stream till end
result = reader.ReadToEnd();
}
}
}
return result;

}

The decrypt method uses the same signature as the encrypt method. However, instead of creating an encryptor class, we create a decryptor class, which implements the ICryptoTransform interface.

The following is the main program, where we create SymmetricAlgorithm of the AESManaged instance type, and then pass it to the encrypt and decrypt methods:

 static void Main(string[] args)
{
Console.WriteLine("Using AES symmetric Algorithm");
SymmetricAlgorithm sem = new AesManaged();
Console.WriteLine("Encrypting data from inputfile");
EncryptDecryptHelper.EncryptSymmetric(sem);
Console.WriteLine("Data Encrypted. You can check in outputfile. Press any key to decrypt message");
System.Console.ReadKey();
Console.WriteLine("Decrypting content form outputfile");
string message = EncryptDecryptHelper.DecryptSymmetric(sem);
Console.WriteLine($"Decrypted data : {message}");
// Keep the console window open in debug mode.
System.Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}

Before executing the program, check the input file in the solution where you can change the content and execute it. Following encryption, the output file in the solution can be verified for the encrypted content:

When you practice this example, make sure that you have all the helper methods in one class, and the main method in another class, as specified in the sample code on GitHub. Now that you have all the methods, when you execute them, you see the preceding output.

Your encryption method uses the AES algorithm. It reads data from the input file, encrypts data using the AES algorithm, and then writes a message on the screen. Once you press any key, your decrypt method is initiated, decrypts the message, and writes to the output file. The same message is displayed on screen.

In a real-time scenario, when you want to perform secure transactions using file transfers, this is one way to do so. Because you will be using symmetric algorithms, it will be easy to encrypt or decrypt the content.

A sender encrypts the content of the file and sends it to the receiver. The receiver decrypts the file content and processes it. In this method, both the sender and receiver should be aware of the key used.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.79.33