DES in C

Associate
Joined
19 Dec 2005
Posts
641
Location
Perth, Western Australia
Hi,

I have not programmed for at least 6+ years and am looking to try to get back into it.

How can i implement the encryption and decryption as used in DES using C.

The program should ideally receive a message or open a file to encrypt it or decrypt it. it should also receive a DES key.
Both encryption and decryption operations are the same with one exception in the sequence in which the sub keys are used. If in encryption subkeys 1 to 16 are used then in encryption the subkeys 16 to 1 are used.

The program should receive a key and a file to encrypt and it should produce a encrypted file. It should also have some option to receive the encrypted file and they key, and it should decrypt it and produce the original file again.

Any tips or help would be great as i have a lot to learn to get back into programming.
 
cannot use the easy way i think we have to write it totally from scratch even though i dont program and have not for a long time.
 
Also if there is a library that will do the DES encryption for me i am not allowed to use it.

I am using Pelles C for Windows as the compiler.

I guess i need to start by generating the 16 sub-keys from a single key, now as i have never coded in C i am lost at where to start.
 
Taking C out of the picture briefly do you understand how DES works to the point where you can start sketching out rough pseudocode?

Una's link has quite a detailed looking walkthrough with expected output for each step.

Once you know in detail what you want to do people on here should be better able to help you turn it into C code.

At present the problem is a bit too broad and arcane for us to be all that helpful. I may be proved wrong, someone here could have implemented DES before :)
 
I guess i need to start by generating the 16 sub-keys from a single key, now as i have never coded in C i am lost at where to start.

Yeah, ok.. DES uses 56bit keys but you provide it with a 64bit key. In hardware DES those extra 8 bits are used for parity checks but in software you ignore them. So anyhow the first thing you need to do is transform the key using a transformation table. So the 57th bit of the original key K becauses the 1st bit of the permuted key K' . Repeat this till all the bits have been permuted.

Ill start you off with the code for the table. You just need to work out how to index into it and swap according to the values held.

Code:
static const int transform_table[56] = {

   57, 49, 41, 33, 25, 17,  9,  1, 58, 50, 42, 34, 26, 18,
   10,  2, 59, 51, 43, 35, 27, 19, 11,  3, 60, 52, 44, 36,
   63, 55, 47, 39, 31, 23, 15,  7, 62, 54, 46, 38, 30, 22,
   14,  6, 61, 53, 45, 37, 29, 21, 13,  5, 28, 20, 12,  4

};

This is really pointless if you can't write C to a decent standard, its not an easy thing to implement from the maths alone. You should read about bitshifting/bitops because your going to be doing a lot of it.

http://www.cs.umd.edu/class/spring2003/cmsc311/Notes/BitOp/bitshift.html
 
Last edited:
You should pick up Applied Cryptography by Bruce Schneier that should explain how to do it.

Edit: oops just read unas post.
 
You should also be very wary of doing this as anything other then an intellectual exercise. The most common flaw in cryptography isnt the algorithms, its the implementation.
 
Hi, Thanks for the help, The problem is i dont program and have not for 6+ years and never in C.

We have done no programming in any class and all of a sudden he throws this at us. We cannot even use the built in library's.

I have 2 documents that explain what is required better, but cannot attach them to this post.

If anyone wants a look i can email you them
 
Hi Una,

Have you got any sample code about how to get the key into those tables and create subkeys?
 
Last edited:
Back
Top Bottom