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

Luca DomenichiniPosted Jul 14, 2021, 9:15 PM
Hello, every time I use your code I receive a System.FormatException saying "Invalid length for a Base-64 char array". What am I doing wrong?
Dennis GiménezPosted May 22, 2020, 9:14 PM
Thanks for such a nice information
Yogesh UpretiPosted Oct 24, 2019, 2:37 AM
Thanks for such a nice information
vanita nikamPosted Dec 5, 2018, 12:02 AM
Hello arvind singh baghel--I newly going to use encryption .My project need encrypted data to save in database and while displaying it should decrypt again ..and it should be strong encryption techanique..Please help me.
HuB FNAPosted Apr 24, 2018, 9:21 PM
Thanks for your help, but I have a doubt, how can I set keysize in 256 length?
Kakumanu krishnaPosted Apr 11, 2018, 1:31 AM
Hi Arvind Singh Baghel ,can you provide java code for this. I am getting invalid key length. I know it should be 128 bit format. But i am not able to convert.
thulasi ayyaPosted Jan 6, 2018, 2:36 AM
Tell the example key for des encryption
thulasi ayyaPosted Jan 6, 2018, 2:35 AM
Tell the example key pls!
Hrides ThakurPosted Dec 17, 2017, 10:21 PM
Informative article thanks for sharing........
Arvind SinghPosted Nov 19, 2017, 11:51 PM
Thanks @Ramji...............
Rammohan BaghelPosted Nov 17, 2017, 6:45 AM
Nice article, Helpful for me.
Arvind SinghPosted Feb 19, 2017, 11:25 PM
For more details on encryption decryption check on my blog here:https://programingthoughts.wordpress.com/2017/02/17/encryption-and-decryption-in-c-net-aes-des-encryption-algorithms/
Arvind SinghPosted Feb 19, 2017, 11:24 PM
Thanks to all of you.......
Vishal PrajapatiPosted Feb 19, 2017, 10:41 PM
Nice share..Keep up the good work.
Humayun Kabir MamunPosted Feb 19, 2017, 12:29 AM
Thanks for this nice article...
Anupam SinghPosted Feb 18, 2017, 4:13 AM
A good introduction @Arvind .