Introduction To AES And DES Encryption Algorithms In .NET

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. 

public string EncryptData(string strData, string strKey)
{
    byte[] key = { }; // Encryption Key
    byte[] IV = { 10, 20, 30, 40, 50, 60, 70, 80 };
    byte[] inputByteArray;
    try
    {
        key = Encoding.UTF8.GetBytes(strKey);
        // DESCryptoServiceProvider is a cryptography class defined in C#.
        DESCryptoServiceProvider ObjDES = new DESCryptoServiceProvider();
        inputByteArray = Encoding.UTF8.GetBytes(strData);
        MemoryStream Objmst = new MemoryStream();
        CryptoStream Objcs = new CryptoStream(Objmst, ObjDES.CreateEncryptor(key, IV), CryptoStreamMode.Write);
        Objcs.Write(inputByteArray, 0, inputByteArray.Length);
        Objcs.FlushFinalBlock();
        return Convert.ToBase64String(Objmst.ToArray()); // encrypted string
    }
    catch (System.Exception ex)
    {
        throw ex;
    }
}

Decryption code 

public string DecryptData(string strData, string strKey)
{
    byte[] key = { }; // Key
    byte[] IV = { 10, 20, 30, 40, 50, 60, 70, 80 };
    byte[] inputByteArray = new byte[strData.Length];
    try
    {
        key = Encoding.UTF8.GetBytes(strKey);
        DESCryptoServiceProvider ObjDES = new DESCryptoServiceProvider();
        inputByteArray = Convert.FromBase64String(strData);
        MemoryStream Objmst = new MemoryStream();
        CryptoStream Objcs = new CryptoStream(Objmst, ObjDES.CreateDecryptor(key, IV), CryptoStreamMode.Write);
        Objcs.Write(inputByteArray, 0, inputByteArray.Length);
        Objcs.FlushFinalBlock();
        Encoding encoding = Encoding.UTF8;
        return encoding.GetString(Objmst.ToArray());
    }
    catch (System.Exception ex)
    {
        throw ex;
    }
}

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.

AES Advanced Encryption Standard

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

Encryption code Check the comments in the code for more details. 

string EncryptData(string textData, string Encryptionkey)
{
    RijndaelManaged objrij = new RijndaelManaged();
    // Set the mode for operation of the algorithm
    objrij.Mode = CipherMode.CBC;
    // Set the padding mode used in the algorithm
    objrij.Padding = PaddingMode.PKCS7;
    // Set the size, in bits, for the secret key
    objrij.KeySize = 0x80;
    // Set the block size in bits for the cryptographic operation
    objrij.BlockSize = 0x80;
    // Set the symmetric key that is used for encryption & decryption
    byte[] passBytes = Encoding.UTF8.GetBytes(Encryptionkey);
    // Set the initialization vector (IV) for the symmetric algorithm
    byte[] EncryptionkeyBytes = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
    int len = passBytes.Length;
    if (len > EncryptionkeyBytes.Length)
    {
        len = EncryptionkeyBytes.Length;
    }
    Array.Copy(passBytes, EncryptionkeyBytes, len);
    objrij.Key = EncryptionkeyBytes;
    objrij.IV = EncryptionkeyBytes;
    // Creates a symmetric AES object with the current key and initialization vector IV
    ICryptoTransform objtransform = objrij.CreateEncryptor();
    byte[] textDataByte = Encoding.UTF8.GetBytes(textData);
    // Final transform the test string
    return Convert.ToBase64String(objtransform.TransformFinalBlock(textDataByte, 0, textDataByte.Length));
}

Decryption code 

string DecryptData(string EncryptedText, string Encryptionkey)
{
    RijndaelManaged objrij = new RijndaelManaged();
    objrij.Mode = CipherMode.CBC;
    objrij.Padding = PaddingMode.PKCS7;
    objrij.KeySize = 0x80;
    objrij.BlockSize = 0x80;
    byte[] encryptedTextByte = Convert.FromBase64String(EncryptedText);
    byte[] passBytes = Encoding.UTF8.GetBytes(Encryptionkey);
    byte[] EncryptionkeyBytes = new byte[0x10];
    int len = passBytes.Length;
    if (len > EncryptionkeyBytes.Length)
    {
        len = EncryptionkeyBytes.Length;
    }
    Array.Copy(passBytes, EncryptionkeyBytes, len);
    objrij.Key = EncryptionkeyBytes;
    objrij.IV = EncryptionkeyBytes;
    byte[] TextByte = objrij.CreateDecryptor().TransformFinalBlock(encryptedTextByte, 0, encryptedTextByte.Length);
    return Encoding.UTF8.GetString(TextByte); // it will return readable string
}

In the next article, I will explain Asymmetric Encryption RSA, DSA.