Get a Pentest and security assessment of your IT network.

Cyber Security

C# DES Encryption (3CBC)

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

  1. Add necessary namespaces: Make sure your C# project has access to the required libraries.
  2. using System; 
    using System.Security.Cryptography;
    using System.Text;
    
  3. Create a DES object: Instantiate a DESCryptoServiceProvider object. This is your encryption engine.
  4. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    
  5. 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.
  6. byte[] iv = GenerateRandomIV(8); // 8 bytes for DES
    
  7. Generate a Key: DES uses an 8-byte key. Ensure this is kept secret!
  8. byte[] key = GenerateRandomKey(8);
    
  9. Convert the string to bytes: Use UTF8 encoding for consistent results.
  10. string textToEncrypt = "My Secret String";
    byte[] dataToEncrypt = Encoding.UTF8.GetBytes(textToEncrypt);
    
  11. Create a CryptoStream for encryption: This stream handles the actual encryption process.
  12. MemoryStream ms = new MemoryStream();
    CryptoStream cryptoStream = new CryptoStream(ms, des.CreateEncryptor(key, iv), CryptoStreamMode.Write);
    
  13. Write the data to the CryptoStream: This encrypts the data as it’s written.
  14. cryptoStream.Write(dataToEncrypt, 0, dataToEncrypt.Length);
    cryptoStream.FlushFinalBlock();
    
  15. Get the encrypted bytes: Read the contents of the MemoryStream to get the ciphertext.
  16. byte[] cipherTextBytes = ms.ToArray();
    
  17. Convert the encrypted bytes to a string (optional): Base64 encoding is common for representing binary data as text.
  18. string base64CipherText = Convert.ToBase64String(cipherTextBytes);
    

Decrypting a String

  1. Add necessary namespaces: (Same as encryption)
  2. using System; 
    using System.Security.Cryptography;
    using System.Text;
    
  3. Create a DES object: Instantiate a DESCryptoServiceProvider object.
  4. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    
  5. Set the Key and IV: Use the same key and IV that were used for encryption!
  6. byte[] key = GenerateRandomKey(8); // Same as encryption key
    byte[] iv = GenerateRandomIV(8); // Same as encryption IV
    
  7. Convert the Base64 encoded string back to bytes: If you stored the ciphertext as a Base64 string.
  8. string base64CipherText = "YourBase64EncodedString";
    byte[] cipherTextBytes = Convert.FromBase64String(base64CipherText);
    
  9. Create a CryptoStream for decryption: This stream handles the actual decryption process.
  10. MemoryStream ms = new MemoryStream(cipherTextBytes);
    CryptoStream cryptoStream = new CryptoStream(ms, des.CreateDecryptor(key, iv), CryptoStreamMode.Read);
    
  11. Read the data from the CryptoStream: This decrypts the data as it’s read.
  12. byte[] decryptedData = new byte[cipherTextBytes.Length];
    int bytesRead = cryptoStream.Read(decryptedData, 0, decryptedData.Length);
    
  13. Convert the decrypted bytes to a string: Use UTF8 encoding.
  14. 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;
}
Related posts
Cyber Security

Zip Codes & PII: Are They Personal Data?

Cyber Security

Zero-Day Vulnerabilities: User Defence Guide

Cyber Security

Zero Knowledge Voting with Trusted Server

Cyber Security

ZeroNet: 51% Attack Risks & Mitigation