Post

# Introduction To AES And DES Encryption Algorithms In .NET

• 165.9k
• 0
• 14

There are mainly two types of algorithms that are used for encryption and decryption.

## Symmetric encryption

In this type of encryption, a single key is used for encryption and decryption. It is faster than the other but it also has some drawbacks like a single key, which is used for encryption and decryption, so when you encrypt the data, you have to provide the same key for decryption and if the data is sent over the network, then at the end, where decryption happened, we also need to know the same key. Suppose, you have a Service and you're doing encryption/ decryption of the message with a key and your many clients consume that Service, then you have to provide your key to your client also. It needs a very high-level trust, as you are sharing your key, which means your secret.

## Asymmetric encryption

We have seen that Symmetric encryption has some security and trust problems, so Asymmetric encryption solves that problem. Asymmetric encryption uses two keys for encryption and decryption, where one key is for encryption and another key is for decryption. Encrypt message by a public key and decrypt the message by using the private key. The public key is used only for encryption and cannot decrypt the message by the public key but Asymmetric encryption is slower than others. It is very slow, so it does not fit well for the large data even more than 1kilobyte.

Mainly two algorithms are used for the Asymmetric encryption.
1. RSA

RSA was first described in 1978 by Ron Rivest, Adi Shamir, and Leonard Adleman and was named on their name RSA, which stands for Ron Rivest, Adi Shamir, and Leonard Adleman

2. DSA

DSA stands for Digital Signature Algorithm.
We will cover Asymmetric encryption (RSA, DSA) in the next article.

In this article, we will see Symmetric encryption with DES, 3DES, and AES algorithms.

There are many algorithms, which are available for encryption. I will explain three, which are DES, 3DES, AES.

## DES Data Encryption Standard

It’s a traditional old way, which is used for encryption and decryption. It’s not reliable and can break easily. The key size in DES is very short. It’s not very good when our data travels over networks.

Encryption code

Check comments in the code for more details.
1. public string EncryptData(string strData, string strKey)
2.     {
3.         byte[] key = { }; //Encryption Key
4.         byte[] IV = { 10, 20, 30, 40, 50, 60, 70, 80 };
5.         byte[] inputByteArray;
6.
7.         try
8.         {
9.             key = Encoding.UTF8.GetBytes(strKey);
10.             // DESCryptoServiceProvider is a cryptography class defind in c#.
11.             DESCryptoServiceProvider ObjDES = new DESCryptoServiceProvider();
12.             inputByteArray = Encoding.UTF8.GetBytes(strData);
13.        MemoryStream Objmst = new MemoryStream();
14.        CryptoStream Objcs = new CryptoStream(Objmst, ObjDES.CreateEncryptor(key, IV), CryptoStreamMode.Write);
15.        Objcs.Write(inputByteArray, 0, inputByteArray.Length);
16.        Objcs.FlushFinalBlock();
17.
18.            return Convert.ToBase64String(Objmst.ToArray());//encrypted string
19.         }
20.         catch (System.Exception ex)
21.         {
22.            throw ex;
23.         }
24.     }
Decryption code
1. public string DecryptData(string strData, string strKey)
2.     {
3.         byte[] key = { };// Key
4.         byte[] IV = { 10, 20, 30, 40, 50, 60, 70, 80 };
5.         byte[] inputByteArray = new byte[strData.Length];
6.
7.         try
8.         {
9.             key = Encoding.UTF8.GetBytes(strKey);
10.             DESCryptoServiceProvider ObjDES = new DESCryptoServiceProvider();
11.             inputByteArray = Convert.FromBase64String(strData);
12.
13.             MemoryStream Objmst = new MemoryStream();
14.             CryptoStream Objcs = new CryptoStream(Objmst, ObjDES.CreateDecryptor(key, IV), CryptoStreamMode.Write);
15.             Objcs.Write(inputByteArray, 0, inputByteArray.Length);
16.             Objcs.FlushFinalBlock();
17.
18.             Encoding encoding = Encoding.UTF8;
19.             return encoding.GetString(Objmst.ToArray());
20.         }
21.         catch (System.Exception ex)
22.         {
23.             throw ex;
24.         }
25.     }
3DES - Called Triple DES

As we have seen, there are some security-related issues in the DES algorithm, so we can say that 3DES is an updated version of DES. In the 3DES, they also increase the key size, which was very short in DES.

The Advanced Encryption Standard or AES is also called Rijndael cipher. AES supports 128, 192, and 256-bit encryption, which can be determined by the key size, 128-bit encryption key size is 16 bytes, the 192-bit encryption key is 24 bytes and 256-bit encryption key size is 32 bytes. AES Encryption offers good performance and a good level of security. AES Encryption is a symmetric cipher and uses the same key for encryption and decryption.

Encryption code

Check the comments in the code for more details.
1. string EncryptData(string textData, string Encryptionkey)
2.         {
3.             RijndaelManaged objrij = new RijndaelManaged();
4.             //set the mode for operation of the algorithm
5.             objrij.Mode = CipherMode.CBC;
6.             //set the padding mode used in the algorithm.
8.             //set the size, in bits, for the secret key.
9.             objrij.KeySize = 0x80;
10.             //set the block size in bits for the cryptographic operation.
11.             objrij.BlockSize = 0x80;
12.             //set the symmetric key that is used for encryption & decryption.
13.             byte[] passBytes = Encoding.UTF8.GetBytes(Encryptionkey);
14.             //set the initialization vector (IV) for the symmetric algorithm
15.             byte[] EncryptionkeyBytes = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
16.
17.             int len = passBytes.Length;
18.             if (len > EncryptionkeyBytes.Length)
19.             {
20.                 len = EncryptionkeyBytes.Length;
21.             }
22.             Array.Copy(passBytes, EncryptionkeyBytes, len);
23.
24.             objrij.Key = EncryptionkeyBytes;
25.             objrij.IV = EncryptionkeyBytes;
26.
27.             //Creates a symmetric AES object with the current key and initialization vector IV.
28.             ICryptoTransform objtransform = objrij.CreateEncryptor();
29.             byte[] textDataByte = Encoding.UTF8.GetBytes(textData);
30.             //Final transform the test string.
31.             return Convert.ToBase64String(objtransform.TransformFinalBlock(textDataByte, 0, textDataByte.Length));
32.         }
Decryption code
1. string DecryptData(string EncryptedText, string Encryptionkey)
2.         {
3.             RijndaelManaged objrij = new RijndaelManaged();
4.             objrij.Mode = CipherMode.CBC;
6.
7.             objrij.KeySize = 0x80;
8.             objrij.BlockSize = 0x80;
9.             byte[] encryptedTextByte = Convert.FromBase64String(EncryptedText);
10.             byte[] passBytes = Encoding.UTF8.GetBytes(Encryptionkey);
11.             byte[] EncryptionkeyBytes = new byte[0x10];
12.             int len = passBytes.Length;
13.             if (len > EncryptionkeyBytes.Length)
14.             {
15.                 len = EncryptionkeyBytes.Length;
16.             }
17.             Array.Copy(passBytes, EncryptionkeyBytes, len);
18.             objrij.Key = EncryptionkeyBytes;
19.             objrij.IV = EncryptionkeyBytes;
20.             byte[] TextByte = objrij.CreateDecryptor().TransformFinalBlock(encryptedTextByte, 0, encryptedTextByte.Length);
21.             return Encoding.UTF8.GetString(TextByte);  //it will return readable string
22.         }
In the next article, I will explain Asymmetric Encryption RSA, DSA.

Recommended Free Ebook
Similar Articles