Home > Uncategorized > #DES Encryption using #BouncyCastle in C#

#DES Encryption using #BouncyCastle in C#


DES or the Data Encryption Standard, is a classic symetric encryption algorithm that is largely been superceeded by AES and 3DES nowadays, but you may have a legacy format that still uses DES, so you will need to interoperate with it.

This code example uses the BouncyCastle library in C# to perform DES encryption and decryption with an 8 byte (64 bit) key, and is available here; https://github.com/infiniteloopltd/DES

It is a 8 byte block cypher, like Blowfish, so your ciphertext will be bloated by a maximum of 8 bytes – So it’s economicical with memory on short plaintexts.

I’ve used byte arrays here as inputs and outputs for flexibility;

private static byte[] DES_Encrypt(byte[] input, byte[] key)
var engine = new DesEngine();
var cipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(engine));
cipher.Init(true, new KeyParameter(key));
var cipherText = new byte[cipher.GetOutputSize(input.Length)];
var outputLen = cipher.ProcessBytes(input, 0, input.Length, cipherText, 0);
cipher.DoFinal(cipherText, outputLen);
return cipherText;

And the decyption is as follows;

private static byte[] DES_Decrypt(byte[] key, byte[] cipherText)
var engine = new DesEngine();
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(engine));
cipher.Init(false, new KeyParameter(key));
var plainText = new byte[cipher.GetOutputSize(cipherText.Length)];
var outputLen = cipher.ProcessBytes(cipherText, 0, cipherText.Length, plainText, 0);
cipher.DoFinal(plainText, outputLen);
return plainText;


Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: