473,387 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

DES implementation in C

I have been asked to do a DES encryption project in C but pretty new to programming. I've found the following code in C++ but am not sure how to do the equivalent of classes in C.

I dont know the syntax of how to move from one section of code to the next. Do i have to declare seperate functions for each section even if its only a simple for loop section of code? for example how would i move from:

void Des::IP() //Initial Permutation -----> void Des::Expansio() //Permutation Choice-1

in code below:

Expand|Select|Wrap|Line Numbers
  1. # include <stdio.h>
  2. # include <fstream.h>
  3. # include <string.h>
  4. # include <conio.h>
  5. # include <iostream.h>
  6.     int key[64]={
  7.         0,0,0,1,0,0,1,1,
  8.         0,0,1,1,0,1,0,0,
  9.         0,1,0,1,0,1,1,1,
  10.         0,1,1,1,1,0,0,1,
  11.         1,0,0,1,1,0,1,1,
  12.         1,0,1,1,1,1,0,0,
  13.         1,1,0,1,1,1,1,1,
  14.         1,1,1,1,0,0,0,1
  15.         };
  16. class Des
  17. {
  18.  public:
  19.   int keyi[16][48],
  20.       total[64],
  21.       left[32],
  22.       right[32],
  23.       ck[28],
  24.       dk[28],
  25.       expansion[48],
  26.       z[48],
  27.       xor1[48],
  28.       sub[32],
  29.       p[32],
  30.       xor2[32],
  31.       temp[64],
  32.       pc1[56],
  33.       ip[64],
  34.       inv[8][8];
  35.  
  36.   char final[1000];
  37.   void IP();
  38.   void PermChoice1();
  39.   void PermChoice2();
  40.   void Expansion();
  41.   void inverse();
  42.   void xor_two();
  43.   void xor_oneE(int);
  44.   void xor_oneD(int);
  45.   void substitution();
  46.   void permutation();
  47.   void keygen();
  48.   char * Encrypt(char *);
  49.   char * Decrypt(char *);
  50. };
  51. void Des::IP() //Initial Permutation
  52. {
  53.     int k=58,i;
  54.     for(i=0;i<32;i++)
  55.     {
  56.        ip[i]=total[k-1];
  57.        if(k-8>0)  k=k-8;
  58.        else       k=k+58;
  59.     }
  60.     k=57;
  61.     for( i=32;i<64;i++)
  62.     {
  63.         ip[i]=total[k-1];
  64.         if(k-8>0)   k=k-8;
  65.         else        k=k+58;
  66.     }
  67. }
  68. void Des::PermChoice1() //Permutation Choice-1
  69. {
  70.     int k=57,i;
  71.     for(i=0;i<28;i++)
  72.     {
  73.         pc1[i]=key[k-1];
  74.         if(k-8>0)    k=k-8;
  75.         else         k=k+57;
  76.     }
  77.     k=63;
  78.     for( i=28;i<52;i++)
  79.     {
  80.         pc1[i]=key[k-1];
  81.         if(k-8>0)    k=k-8;
  82.         else         k=k+55;
  83.     }
  84.     k=28;
  85.     for(i=52;i<56;i++)
  86.     {
  87.         pc1[i]=key[k-1];
  88.         k=k-8;
  89.     }
  90.  
  91. }
  92.  
  93.  
Mar 21 '10 #1
4 19985
mac11
256 100+
There are free and *proven* libraries available. I would avoid implementing (or modifying) my own encryption like the plague - unless you have some very good reason (or you just want to do it for fun?).

Why are you trying to convert C++ to C? Are you trying to put encryption into some embedded system for which you have no C++ compiler?
Mar 22 '10 #2
weaknessforcats
9,208 Expert Mod 8TB
In C++ classes are implemented as structs.

Therefore, in C use a struct for your class.

For your member funcitons, use function pointers in your struct that contain the address of the correct function to use with that struct. Be sure the first argument of your functions is a pointer to struct variable do the function knows where the data is located (this pointer),

Any inheritance will need to be done manually usually by having an embeded struct variable( the base class) as a member if the struct.

BTW: Is there a point to this or is this just an exercise to make you do work normaslly done by a compiler?
Mar 22 '10 #3
Hi guys, thanks for replies..Im doing the implementation as part of a college assignment..

weaknessforcats: If i can id rather just stick to using function prototypes and definitions as i want to keep it as simple as possible if i can at the moment..This is what ive tried for the code below but it only seems to be compiling the code directly within the main block and not the void printtable function ..Do i have to call the function and if so where do i call it (within main??)..Please excuse my ignorance (prob a very stupid question for you) but i am new to programming and if you could tell me what im doing wrong id much appreciate it..

#include <stdio.h>

void printtable(int xsize,int ysize,
float table[][5]); /* Function prototype */
int main(void)
{
int i, j, m = 0, IP[64],P[64] = {0,1,0,0,0,1,0,0,
0,1,1,1,0,1,0,0,
0,1,0,1,0,1,0,0,
0,1,1,1,0,1,0,0,
0,1,0,0,0,1,0,0,
0,1,1,1,0,1,0,0,
0,1,0,0,0,1,0,0,
0,1,1,1,0,1,0,1,};


/*INITIAL PERMUTATION*/
int k = 57;
for (i = 0; i<4; ++i) {
for (j=k; j>=1; j=j-8){
IP[m]=P[j];
printf("%2d", IP[m]);
++m;
}
k=k+2;
}

printf("\n");

k = k-9;
for (i = 0; i<4; ++i) {
for (j=k; j>=0; j=j-8){
IP[m]=P[j];
printf("%2d", IP[m]);
++m;
}
k=k+2;
}
}
void printtable(int xsize,int ysize,
float table[][5]) /* Function definition */
{
int x,y;
for (x=0;x<xsize;x++)
{ for (y=0;y<ysize;y++)
printf("t%f",table[x][y]);
printf("n");
}
}
Mar 23 '10 #4
weaknessforcats
9,208 Expert Mod 8TB
All of your code has been compiled.

Your program starts with the { of main() and ends with the } of main(). Therefore, if you do not call the function between those braces, it is never called.
Mar 24 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: jenniferyiu | last post by:
IMHO, simply NO. False actually, practically.
9
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
29
by: Enrico `Trippo' Porreca | last post by:
Both K&R book and Steve Summit's tutorial define a getline() function correctly testing the return value of getchar() against EOF. I know that getchar() returns EOF or the character value cast to...
52
by: lovecreatesbeauty | last post by:
Why the C standard committee doesn't provide a standard implementation including the C compiler and library when the language standard document is published? C works on the abstract model of low...
20
by: Luc Kumps | last post by:
(Sorry about the previous post, it got transmitted before it was complete) We try to separate implementation and interface defintions, but we run into a problem. I hope the guru's can solve this,...
7
by: desktop | last post by:
I the C++ standard page 472 it says that an associative container can be constructed like X(i,j,c) where i and j are input iterators to elements. But in the implementation there is no constructor...
6
by: Ralph | last post by:
Hi, I was reading effictive C++ and some other books again and they all tell you about hiding implementation details (proxy/pimpl/inheritance) but they never really explain when to use it. I...
0
by: anto.anish | last post by:
Hi , Since, i did not want to write instantiations in Source file of all template methods for various different datatypes that my client might use, i choose to write implementation of template...
1
by: anto.anish | last post by:
Hi , Since, i did not want to write explicit instantiations in Source file of all template methods for various different datatypes that my client might use, i choose to write implementation of...
173
by: Ron Ford | last post by:
I'm looking for a freeware c99 compiler for windows. I had intended to use MS's Visual C++ Express and use its C capability. In the past with my MS products, I've simply needed to make .c the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.