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: - # 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;
-
}
-
-
}
-
-
4 19985
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?
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?
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");
}
}
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: jenniferyiu |
last post by:
IMHO, simply NO.
False actually, practically.
|
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
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
| |