DEV Community

Cover image for Symmetric v/s Asymmetric Encryption
Sukhpinder Singh
Sukhpinder Singh

Posted on

Symmetric v/s Asymmetric Encryption

C# CONCEPTS

Symmetric encryption is performed on streams whereas asymmetric encryption works on a small number of bytes.

What is Encryption?

Encryption means the conversion of data to symbols so that its contents cannot be understood if intercepted.

There are mainly two types of encryption techniques:

  • Symmetric Encryption

  • Asymmetric Encryption

What is Symmetric Encryption?

Symmetric encryption is executed on streams and therefore, it’s useful to encrypt vast volumes of data.

Let us consider an example of writing encrypted text into a .txt file and understand symmetric encryption step by step.

Create a file stream

Create a new file stream with OpenOrCreate file mode.

FileStream myStream = new FileStream("test.txt", FileMode.OpenOrCreate);
Enter fullscreen mode Exit fullscreen mode

AES instance

Build a unique instance of the default Aes implementation class and encrypt the stream.

Aes aes = Aes.Create();

byte[] key = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };

byte[] iv = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
Enter fullscreen mode Exit fullscreen mode

Create a Crypto Stream

Build a CryptoStream, pass it the FileStream, and encrypt it with the Aes class and pass onto StreamWriter.

CryptoStream cryptStream = new CryptoStream(

myStream,

aes.CreateEncryptor(key, iv),

CryptoStreamMode.Write);
Enter fullscreen mode Exit fullscreen mode

Create a StreamWriter for quickly write to the file stream.

StreamWriter sWriter = new StreamWriter(cryptStream);
Enter fullscreen mode Exit fullscreen mode

Write to the stream & close connections.

sWriter.WriteLine("Hello World!");

sWriter.Close();cryptStream.Close();myStream.Close();
Enter fullscreen mode Exit fullscreen mode

Complete Code

What is Asymmetric Encryption?

Asymmetric encryption is done on a small number of bytes and is therefore beneficial only for small amounts of data.

Initialize Public Key

Create a byte arrays to the public key information.

byte[] modulus = {214,46,220,83,160,73,40,39,201,155,19,202,3,11,191,178,56,

74,90,36,248,103,18,144,170,163,145,87,54,61,34,220,222,

207,137,149,173,14,92,120,206,222,158,28,40,24,30,16,175,

108,128,35,230,118,40,121,113,125,216,130,11,24,90,48,194,

240,105,44,76,34,57,249,228,125,80,38,9,136,29,117,207,139,

168,181,85,137,126,10,126,242,120,247,121,8,100,12,201,171,

38,226,193,180,190,117,177,87,143,242,213,11,44,180,113,93,

106,99,179,68,175,211,164,116,64,148,226,254,172,147};

byte[] exponent = { 1, 0, 1 };
Enter fullscreen mode Exit fullscreen mode

Create an RSA class instance

Build a new instance of the RSA class.

RSA rsa = RSA.Create();
Enter fullscreen mode Exit fullscreen mode

Create RSA parameters & import key

Build a new instance of the RSAParameters structure as shown below, Set rsaKeyInfo to the public key values and Import key parameters into RSA.

RSAParameters rsaKeyInfo = new RSAParameters();rsaKeyInfo.Modulus = modulus;rsaKeyInfo.Exponent = exponent;

rsa.ImportParameters(rsaKeyInfo);
Enter fullscreen mode Exit fullscreen mode

AES class & Encryption

Build a new instance of the default Aes implementation class and Encrypt the symmetric key and IV.

Aes aes = Aes.Create();

encryptedKey = rsa.Encrypt(aes.Key, RSAEncryptionPadding.Pkcs1);

encryptedIV = rsa.Encrypt(aes.IV, RSAEncryptionPadding.Pkcs1);
Enter fullscreen mode Exit fullscreen mode

Complete Code

Thank you for reading. I hope you like the article..!!

Top comments (0)