DES implementation in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oggiemc
    New Member
    • Mar 2010
    • 2

    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:

    Code:
    # include <stdio.h>
    # include <fstream.h>
    # include <string.h>
    # include <conio.h>
    # include <iostream.h>
    	int key[64]={
    		0,0,0,1,0,0,1,1,
    		0,0,1,1,0,1,0,0,
    		0,1,0,1,0,1,1,1,
    		0,1,1,1,1,0,0,1,
    		1,0,0,1,1,0,1,1,
    		1,0,1,1,1,1,0,0,
    		1,1,0,1,1,1,1,1,
    		1,1,1,1,0,0,0,1
    		};
    class Des
    {
     public:
      int keyi[16][48],
          total[64],
          left[32],
          right[32],
          ck[28],
          dk[28],
          expansion[48],
          z[48],
          xor1[48],
          sub[32],
          p[32],
          xor2[32],
          temp[64],
          pc1[56],
          ip[64],
          inv[8][8];
    
      char final[1000];
      void IP();
      void PermChoice1();
      void PermChoice2();
      void Expansion();
      void inverse();
      void xor_two();
      void xor_oneE(int);
      void xor_oneD(int);
      void substitution();
      void permutation();
      void keygen();
      char * Encrypt(char *);
      char * Decrypt(char *);
    };
    void Des::IP() //Initial Permutation
    {
    	int k=58,i;
    	for(i=0;i<32;i++)
    	{
    	   ip[i]=total[k-1];
    	   if(k-8>0)  k=k-8;
    	   else       k=k+58;
    	}
    	k=57;
    	for( i=32;i<64;i++)
    	{
    		ip[i]=total[k-1];
    		if(k-8>0)   k=k-8;
    		else	    k=k+58;
    	}
    }
    void Des::PermChoice1() //Permutation Choice-1
    {
    	int k=57,i;
    	for(i=0;i<28;i++)
    	{
    		pc1[i]=key[k-1];
    		if(k-8>0)    k=k-8;
    		else	     k=k+57;
    	}
    	k=63;
    	for( i=28;i<52;i++)
    	{
    		pc1[i]=key[k-1];
    		if(k-8>0)    k=k-8;
    		else         k=k+55;
    	}
    	k=28;
    	for(i=52;i<56;i++)
    	{
    		pc1[i]=key[k-1];
    		k=k-8;
    	}
    
    }
  • mac11
    Contributor
    • Apr 2007
    • 256

    #2
    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?

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      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?

      Comment

      • oggiemc
        New Member
        • Mar 2010
        • 2

        #4
        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",ta ble[x][y]);
        printf("n");
        }
        }

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          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.

          Comment

          Working...