Data Encryption Standard (DES) in C#

DES (Data Encryption Standard) is a symmetric-key block cipher algorithm that operates on 64-bit blocks of data. Below is a step-by-step guide on implementing DES encryption in C# with examples:

DES implementation in C#

Import the necessary namespace
using System.Security.Cryptography;
Create a DESCryptoServiceProvider object
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
Convert your plaintext and key to byte arrays
string plaintext = "Your plaintext message"; byte[] plaintextBytes = Encoding.UTF8.GetBytes(plaintext); string key = "Your 8-byte key"; // DES requires a 64-bit (8-byte) key byte[] keyBytes = Encoding.UTF8.GetBytes(key);
Create an encryptor object
ICryptoTransform encryptor = des.CreateEncryptor(keyBytes, keyBytes);
Create a MemoryStream to hold the encrypted data:
MemoryStream ms = new MemoryStream();
Create a CryptoStream to perform the encryption
CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write);
Write the plaintext to the crypto stream
cs.Write(plaintextBytes, 0, plaintextBytes.Length);
Flush the final block and close the streams
cs.FlushFinalBlock(); cs.Close();
Retrieve the encrypted bytes and optionally encode them
byte[] encryptedBytes = ms.ToArray(); string encryptedText = Convert.ToBase64String(encryptedBytes); // Optional for text representation
Example of encryption
string encrypted = Encrypt("Your key", "Your message"); // encrypted will hold the base64-encoded ciphertext

Decryption follows a similar process:

Create a decryptor object
ICryptoTransform decryptor = des.CreateDecryptor(keyBytes, keyBytes);
Decode the encrypted text (if it was base64-encoded)
byte[] encryptedBytes = Convert.FromBase64String(encryptedText);
Create a MemoryStream to hold the decrypted data
MemoryStream ms = new MemoryStream();
Create a CryptoStream to perform the decryption
CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write);
Write the plaintext to the crypto stream
cs.Write(encryptedBytes, 0, encryptedBytes.Length);
Flush the final block and close the streams
cs.FlushFinalBlock(); cs.Close();
Retrieve the decrypted bytes and convert them to a string
byte[] decryptedBytes = ms.ToArray(); string decryptedText = Encoding.UTF8.GetString(decryptedBytes);
Example of decryption
string decrypted = Decrypt("Your key", encryptedText); // decrypted will hold the original plaintext
Important notes:
  1. DES is considered a weak algorithm for modern security standards. It's recommended to use stronger algorithms like AES whenever possible.
  2. Proper key management is crucial for encryption security.
  3. Always dispose of cryptographic objects using using statements or by calling Dispose() manually.
Full Source | C#
using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace DESEncryptionExample { class Program { static void Main(string[] args) { string plaintext = "This is the secret message."; string key = "12345678"; // 8-byte key for DES // Encryption string encryptedText = Encrypt(key, plaintext); Console.WriteLine("Encrypted text: {0}", encryptedText); // Decryption string decryptedText = Decrypt(key, encryptedText); Console.WriteLine("Decrypted text: {0}", decryptedText); } static string Encrypt(string key, string plaintext) { using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) { byte[] plaintextBytes = Encoding.UTF8.GetBytes(plaintext); byte[] keyBytes = Encoding.UTF8.GetBytes(key); using (ICryptoTransform encryptor = des.CreateEncryptor(keyBytes, keyBytes)) { using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) { cs.Write(plaintextBytes, 0, plaintextBytes.Length); cs.FlushFinalBlock(); } return Convert.ToBase64String(ms.ToArray()); } } } } static string Decrypt(string key, string encryptedText) { using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) { byte[] keyBytes = Encoding.UTF8.GetBytes(key); byte[] encryptedBytes = Convert.FromBase64String(encryptedText); using (ICryptoTransform decryptor = des.CreateDecryptor(keyBytes, keyBytes)) { using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write)) { cs.Write(encryptedBytes, 0, encryptedBytes.Length); cs.FlushFinalBlock(); } return Encoding.UTF8.GetString(ms.ToArray()); } } } } } }
Output:
Encrypted text: CCtDfQOdCUGOINyd4dr6ftbaXGM1t4lQ8XCVCcdRIdk= Decrypted text: This is the secret message.

The above C# code in the DESEncryptionExample namespace demonstrates the implementation of DES encryption and decryption for a given plaintext message using an 8-byte key. In the Main method, a sample message is encrypted using the Encrypt function, and the resulting ciphertext is printed. Subsequently, the Decrypt function is invoked to decrypt the ciphertext back to the original message, which is also displayed.

The Encrypt and Decrypt methods utilize the DESCryptoServiceProvider class to perform the DES encryption and decryption operations, converting the plaintext and ciphertext between byte arrays and Base64-encoded strings.

Conclusion

DES (Data Encryption Standard) is a symmetric-key block cipher algorithm that operates on 64-bit blocks of data using an 8-byte key. It involves a series of substitutions and permutations to encrypt and decrypt messages, and while historically significant, DES is now considered insecure for modern cryptographic applications due to its relatively short key length.