10 Replies - 42384 Views - Last Post: 01 April 2008 - 05:47 AM Rate Topic: -----

#1 smelf1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 19-March 08

DES in C

Posted 19 March 2008 - 08:50 AM

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/code and help would be great as i have alot to learn to get back into programming.

Is This A Good Question/Topic? 0
  • +

Replies To: DES in C

#2 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: DES in C

Posted 19 March 2008 - 08:59 AM

Welcome to </dream.in.code> and back to the programming world :)

Well, if you can give it a try, I think you will be surprised that you still have it in you. As always, if you run into any errors, just point them out and explain them, and we will surely help you out, so happy coding then! :)
Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: DES in C

Posted 19 March 2008 - 09:05 AM

Well my referance for this would be "Applied Cryptography: Protocols, Algorithms, and Source Code in C" by Bruce Schneier (you can generally find this book in the public library system -- and it is not very expensive (as long as it is in print)). This contains a full listing of a DES program in C. If you are interested in cryptography (in C) I highly recommend this book.

You can also find quite a bit of information on DES about the web:
The DES Algorithm Illustrated
Data Encryption Standard on Wikipedia

if your programming in C++ rather than C you can find the source for just about every cryptographic protocol out there in the Crypto++ library (open source).


Other than that:

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Post your code like this: :code:

Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 smelf1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 19-March 08

Re: DES in C

Posted 20 March 2008 - 02:37 AM

Hi,

Thanks for the tips, i have tried but so far i cannot even get hello world to work : )-.

I have not programmed in years and never in C. Normally a masters in IT has no programming and just a huge thesis or research project. But these plebs have slapped us with this as an assignment. I have 2 weeks to now learn C and write a program using DES to encrypt/decrypt.
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: DES in C

Posted 20 March 2008 - 06:18 AM

Well, the good news is that there is not a specific reason to have to learn the DES algorithm. As I have mentioned in my previous post there exists open source libraries that contain the functions that you will require. It really helps to have a good idea of how the algorithm works, but you don't really have to understand the code (DES actually has some *magic* in the form of something called S-Boxes which scramble the bits -- I had a very hard time understanding DES and I have never actually written the DES algorithm).

So all you really need to do is get up and running on C/C++. I would actually recommend using C++ for your program (especially since you can then use the crypto++ library).

Perhaps we can help you get up and running: What compiler are you using, and on what platform?
Was This Post Helpful? 0
  • +
  • -

#6 smelf1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 19-March 08

Re: DES in C

Posted 20 March 2008 - 07:39 AM

Hi Nickdmax,

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.

This post has been edited by smelf1: 20 March 2008 - 08:17 AM

Was This Post Helpful? 0
  • +
  • -

#7 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: DES in C

Posted 20 March 2008 - 08:27 AM

You could start with the tutorials and snippets on this website.

If you don't find what your are looking for here, try the tutorials and explanations on this sites :

http://www.cplusplus.com/
http://www.cprogramm...m/tutorial.html

As always, we will be glad to help you with your problems.
Was This Post Helpful? 0
  • +
  • -

#8 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: DES in C

Posted 20 March 2008 - 09:03 AM

:/ owch. Unfortunatly you are using a compiler I know nothing about so I can't really help you even get "hello world" working...

#include <stdio.h>
#include <stdlib.h>

int main() {
	puts("Hello World");
	return 0;
}


Save this to: helloworld.c and try to compile it.

your first step is to get your feet under you. Get set up so that you can compile and run a program. Next you will want to learn about IO since you will need to input both the key and the data. Specifically you should use file IO since DES is a block cypher (as apposed to a character or stream cypher). It will be easier to work with files at first.

Your goal would be to have a program that worked with command line options like:

encryptDES clearfile keyfile cypherfile

or perhapse

doDES /en clearfile keyfile cypherfile
doDES /de cupherfile keyfile clearfile

(so you can specify which way the keys should be used).

once you know how to get the data into your program, then work on breaking the key up, then work on the encryption.
Was This Post Helpful? 0
  • +
  • -

#9 smelf1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 19-March 08

Re: DES in C

Posted 25 March 2008 - 02:48 AM

are there any examples about how to get subkeys from a key?

If i can get this i can then try to figure out how to do it 16 times.
Was This Post Helpful? 0
  • +
  • -

#10 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: DES in C

Posted 25 March 2008 - 08:15 AM

Well I don't really know exactly what you mean since I have not looked at DES in a long time but it is not hard to find source code examples to look at. Heck I googled "DES C" and got this. right off the bat.

However there are sites like Google Code Search and Koders where you can try your luck at finding some examples.

Generally speaking searching code sucks though. Basically you are hoping that the code is commented well enough to aid in your search. Generally I tend to try 5-6 searches before I really find enough data to piece together what I am looking for. I also found that sometimes searching for a keyword in french or spanish helps (though this code can be really obfuscated by the foreign language naming conventions).

If you find something interesting in a project, poke about the other files for that project before returning to your search.
Was This Post Helpful? 0
  • +
  • -

#11 smelf1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 19-March 08

Re: DES in C

Posted 01 April 2008 - 05:47 AM

The program should ideally receive a message or open a file to encrypt it or decrypt it. it should also receive a DES key.

I might use a hard coded key
unsigned char key[8] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};


Below is the code for the table how can i index into it and swap according to the values held.


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 post has been edited by smelf1: 01 April 2008 - 09:11 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1