DES in Haskell

August 25, 2009

I implemented the Data Encryption Standard (DES) encryption algorithm for a Cryptography class I took in college.

I decided to use Haskell since I was learning it at the time. To run (under Windows, and assuming you have GHC installed) double-click DES.hs and you can then use the following functions:

encrypt “plaintext” “key”

decrypt “plaintext” “key”

Both plaintext and key are hexadecimal strings of 16 characters each, equivalent to 64-bit binary blocks.

Sample usage:

*DES> encrypt “675A69675E5A6B5A” “5B5A57676A56676E”
*DES> decrypt “974affbf86022d1f” “5B5A57676A56676E”

Most of the constant data like the S-boxes and shift schedule was taken verbatim from the post Feistel Ciphers and DES in Haskell at codeland. However there was a problem with some of the constant data in the post (I think it was one of the permutations) that led me to the DES specification for the proper data.

Keep in mind that the code isn’t optimized at all, for example, look at the following line:

— drop first bit and last bit
c = binToDec (reverse (drop 1 (reverse (drop 1 input))))

That is obviously a foolish way to chop of the first and the last bit, but hey, I was a beginner at the time.

You can download the code here.


Leave a Reply

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

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

Google+ photo

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

Twitter picture

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

Facebook photo

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


Connecting to %s