TL;DR
This guide shows you how to encrypt and decrypt strings using Data Encryption Standard (DES) in Cipher Block Chaining (CBC) mode in C#. It includes code examples for both encryption and decryption, along with explanations of the key steps involved.
Encrypting a String
- Add necessary namespaces: Make sure your C# project has access to the required libraries.
- Create a DES object: Instantiate a
DESCryptoServiceProviderobject. This is your encryption engine. - Generate an Initialization Vector (IV): The IV must be random and unique for each encryption operation. Store it securely, as you’ll need it to decrypt.
- Generate a Key: DES uses an 8-byte key. Ensure this is kept secret!
- Convert the string to bytes: Use UTF8 encoding for consistent results.
- Create a CryptoStream for encryption: This stream handles the actual encryption process.
- Write the data to the CryptoStream: This encrypts the data as it’s written.
- Get the encrypted bytes: Read the contents of the MemoryStream to get the ciphertext.
- Convert the encrypted bytes to a string (optional): Base64 encoding is common for representing binary data as text.
using System;
using System.Security.Cryptography;
using System.Text;
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] iv = GenerateRandomIV(8); // 8 bytes for DES
byte[] key = GenerateRandomKey(8);
string textToEncrypt = "My Secret String";
byte[] dataToEncrypt = Encoding.UTF8.GetBytes(textToEncrypt);
MemoryStream ms = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(ms, des.CreateEncryptor(key, iv), CryptoStreamMode.Write);
cryptoStream.Write(dataToEncrypt, 0, dataToEncrypt.Length);
cryptoStream.FlushFinalBlock();
byte[] cipherTextBytes = ms.ToArray();
string base64CipherText = Convert.ToBase64String(cipherTextBytes);
Decrypting a String
- Add necessary namespaces: (Same as encryption)
- Create a DES object: Instantiate a
DESCryptoServiceProviderobject. - Set the Key and IV: Use the same key and IV that were used for encryption!
- Convert the Base64 encoded string back to bytes: If you stored the ciphertext as a Base64 string.
- Create a CryptoStream for decryption: This stream handles the actual decryption process.
- Read the data from the CryptoStream: This decrypts the data as it’s read.
- Convert the decrypted bytes to a string: Use UTF8 encoding.
using System;
using System.Security.Cryptography;
using System.Text;
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] key = GenerateRandomKey(8); // Same as encryption key
byte[] iv = GenerateRandomIV(8); // Same as encryption IV
string base64CipherText = "YourBase64EncodedString";
byte[] cipherTextBytes = Convert.FromBase64String(base64CipherText);
MemoryStream ms = new MemoryStream(cipherTextBytes);
CryptoStream cryptoStream = new CryptoStream(ms, des.CreateDecryptor(key, iv), CryptoStreamMode.Read);
byte[] decryptedData = new byte[cipherTextBytes.Length];
int bytesRead = cryptoStream.Read(decryptedData, 0, decryptedData.Length);
string decryptedText = Encoding.UTF8.GetString(decryptedData, 0, bytesRead);
Important Considerations
- Key Management: The security of DES relies entirely on keeping the key secret. Never hardcode keys directly into your application. Use secure storage mechanisms (e.g., configuration files with restricted access, key vaults).
- IV Handling: Always use a unique IV for each encryption operation. Store it alongside the ciphertext so you can decrypt correctly.
- DES is outdated: DES is considered weak by modern standards due to its small key size (56 bits). Consider using stronger algorithms like AES if security is critical.
Helper Functions
private static byte[] GenerateRandomIV(int size)
{
byte[] iv = new byte[size];
using (var rng = RandomNumberGenerator.Create())
{
rng.GetBytes(iv);
}
return iv;
}
private static byte[] GenerateRandomKey(int size)
{
byte[] key = new byte[size];
using (var rng = RandomNumberGenerator.Create())
{
rng.GetBytes(key);
}
return key;
}

