I got this encryption code from the net. Since I generally use unicode, i wish to re-write it for 16 bit character. I just don't understand how the values of variables P10[10] and P8[8] where gotten. Do they follow a particular sequence or they are randomly generated.
The above question has been answered . I used the code it worked fine but soon exhausted the heap because a lot of pointers were dynamically allocated without being release.
I tried to write a destructor for it. but was unsuccessful.I also tried other points for releasing the allOcated memory but keep getting exception thrown by the compiler. Please can any body help me with THE RELEASING OF ALL DYNAMICALLY ALLOCATED MEMORY?
What do I do to make it work for wide char type?
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<assert.h>
void mainmenu(int *);
void menuEn();
void menuDe();
int DoEnDe(int);
class SDES
{
private:
char KEY[11],K1[9],K2[9],IPOutput[9],InvIPOutput[9];
char F1Output[9],F2Output[9];
char INPUT_BIT[9],OUTPUT_BIT[9];
public:
unsigned char INPUT,OUTPUT;
SDES(char *key);
~SDES();
void GenerateKeys();
char *Left_Shift(char *,int );
void conv_to_bits(unsigned char );
void IP(char *);
void InvIP(char *);
void DES_Encryption(unsigned char );
void DES_Decryption(unsigned char );
void Function_F(char *,char *,int );
char *EX_OR(char *,int );
char *SBOX0(char *);
char *SBOX1(char *);
void SDES::GetChar();
};
SDES::SDES(char *key) {
int i;
if (strlen(key)!=10) {
printf("\nInValid Key-Length %s %d",key,strlen(key));
getch();
exit(1);
}
for (i=0;i<10;i++) {
KEY[i]=key[i];
}
KEY[10]='\0';
GenerateKeys(); }
void SDES::GenerateKeys()
{
int P10[10]={3,5,2,7,4,10,1,9,8,6}; char P10_OP[11]; int P8[8]={6,3,7,4,8,5,10,9}; char *P10LEFT,*pl,*pl1,*P10RIGHT,*pr,*pr1,*plpr;
int i;
for (i=0;i<10;i++)
P10_OP[i]=KEY[P10[i]-1];
P10_OP[10]='\0';
for (i=0;i<5;i++)
{
P10LEFT[i]=P10_OP[i];
P10RIGHT[i]=P10_OP[i+5];
}
P10LEFT[5]='\0';
P10RIGHT[5]='\0';
pl=new char[6];
pr=new char[6];
pl=Left_Shift(P10LEFT,1);
pr=Left_Shift(P10RIGHT,1);
for (i=0;i<5;i++)
{
plpr[i]=pl[i];
plpr[i+5]=pr[i];
}
plpr[10]='\0';
for (i=0;i<8;i++)
K1[i]=plpr[P8[i]-1];
K1[8]='\0';
pl1=Left_Shift(pl,2);
pr1=Left_Shift(pr,2);
for (i=0;i<5;i++)
{
plpr[i]=pl1[i];
plpr[i+5]=pr1[i];
}
plpr[10]='\0';
for (i=0;i<8;i++)
{
K2[i]=plpr[P8[i]-1];
}
K2[8]='\0'; }
char *SDES::Left_Shift(char *bs,int n)
{
int length=strlen(bs);
char *char_ptr,firstbit,*str;
char_ptr = new char[length +1];
str=new char[length+1];
char_ptr=bs;
int i,j;
for (j=0;j<n;j++)>
{
firstbit=char_ptr[0];
for (i=0;i<length-1;i++)>
{
str[i]=char_ptr[i+1];
}
str[length-1]=firstbit;
char_ptr[length]='\0';
char_ptr=str;
}
char_ptr[length]='\0';
return(str);
}
void SDES::conv_to_bits(unsigned char ch)
{
int i,bit;
INPUT_BIT[8]='\0';
for (i=7;i>=0;i--)
{
bit=ch%2;
ch=ch/2;
if (bit!=0)
INPUT_BIT[i]='1';
else
INPUT_BIT[i]='0';
}
}
void SDES::IP(char *input)
{
int IPArray[8]={2,6,3,1,4,8,5,7};
int i;
IPOutput[8]='\0';
for (i=0;i<8;i++)
{
IPOutput[i]=input[IPArray[i]-1];
}
}
void SDES::InvIP(char *input)
{
int InvIPArray[8]={4,1,3,5,7,2,8,6};
int i;
InvIPOutput[8]='\0';
for (i=0;i<8;i++)
{
InvIPOutput[i]=input[InvIPArray[i]-1];
}
}
void SDES::DES_Encryption(unsigned char input)
{
char LIP[5],RIP[5],L1[5],R1[5];
int i;
INPUT=input;
conv_to_bits(INPUT); IP(INPUT_BIT); printf("\nEncrpyting.........");
for (i=0;i<4;i++)
{
LIP[i]=IPOutput[i];
RIP[i]=IPOutput[i+4];
}
LIP[4]='\0';
RIP[4]='\0';
Function_F(LIP,RIP,1);
for (i=0;i<4;i++)
{
L1[i]=F1Output[i];
R1[i]=F1Output[4+i];
}
L1[4]='\0';
R1[4]='\0';
Function_F(R1,L1,2);
InvIP(F1Output);
GetChar();
}
void SDES::DES_Decryption(unsigned char input)
{
char LIP[5],RIP[5],L1[5],R1[5];
int i;
INPUT=input;
conv_to_bits(INPUT);
IP(INPUT_BIT); printf("\nDecrpyting.........");
for (i=0;i<4;i++)
{
LIP[i]=IPOutput[i];
RIP[i]=IPOutput[i+4];
}
LIP[4]='\0';
RIP[4]='\0';
Function_F(LIP,RIP,2);
for (i=0;i<4;i++)
{
L1[i]=F1Output[i];
R1[i]=F1Output[4+i];
}
L1[4]='\0';
R1[4]='\0';
Function_F(R1,L1,1);
InvIP(F1Output);
GetChar();
}
void SDES::Function_F(char *linput,char *rinput,int key)
{
int E_P[8]={4,1,2,3,2,3,4,1}; int P4[4]={2,4,3,1}; int i;
char E_POutput[9],*EXOR_Output,*LEXOR,*REXOR;
char *SBOX0_Output,*SBOX1_Output;
char SBOX_Output[5];
char P4_Output[5];
char fk_Output[5];
char Main_Output[9];
for (i=0;i<8;i++)
{
E_POutput[i]=rinput[E_P[i]-1];
}
E_POutput[8]='\0';
EXOR_Output=EX_OR(E_POutput,key);
LEXOR=new char[strlen(EXOR_Output)/2+1];
REXOR=new char[strlen(EXOR_Output)/2+1];
for (i=0;i {
LEXOR[i]=EXOR_Output[i];
REXOR[i]=EXOR_Output[i+4];
}
LEXOR[4]=REXOR[4]='\0';
SBOX0_Output=SBOX0(LEXOR);
SBOX1_Output=SBOX1(REXOR);
for (i=0;i<2;i++)
{
SBOX_Output[i]=SBOX0_Output[i];
SBOX_Output[i+2]=SBOX1_Output[i];
}
SBOX_Output[4]='\0';
for (i=0;i<4;i++)
{
P4_Output[i]=SBOX_Output[P4[i]-1];
}
P4_Output[4]='\0';
for (i=0;i<4;i++)
{
if (P4_Output[i]==linput[i])
fk_Output[i]='0';
else
fk_Output[i]='1';
}
fk_Output[4]='\0';
for (i=0;i<4;i++)
{
Main_Output[i]=fk_Output[i];
Main_Output[i+4]=rinput[i];
}
Main_Output[8]='\0';
strcpy(F1Output,Main_Output);
}
char *SDES::EX_OR(char *ep,int k)
{
char *output,*key;
int i,klen;
output=new char[strlen(ep)+1];
key=new char[strlen(K1)+1];
if (k==1)
{
strcpy(key,K1);
} else
{
if (k==2)
{
strcpy(key,K2);
} else
{
printf("\n\nWrong Choice in the key parameter(1/2)");
getch();
exit(1);
}
}
klen=strlen(K1);
if (strlen(ep)!=klen)
{
printf("\ninput=%d is not equal to K=%d",strlen(ep),klen);
printf("\n\nError in the Output of E/P (Length)..Press any key");
getch();
exit(1);
}
for (i=0;i<strlen(ep);i++)>
{
if (ep[i]==key[i])
output[i]='0';
else
output[i]='1';
}
output[strlen(ep)]='\0';
return(output);
}
char *SDES::SBOX0(char *l)
{
int S0[4][4]={1,0,3,2, 3,2,1,0,
0,2,1,3,
3,1,3,2
};
char *bits[]={"00","01","10","11"};
char lrow[3],lcol[3];
char *SO;
int i,lr,lc,b;
SO=new char[3];
lrow[0]=l[0];
lrow[1]=l[3];
lcol[0]=l[1];
lcol[1]=l[2];
lrow[2]='\0';
lcol[2]='\0';
for (i=0;i<4;i++)
{
if (strcmp(lrow,bits[i])==0)
lr=i;
if (strcmp(lcol,bits[i])==0)
lc=i;
}
b=S0[lr][lc];
for (i=0;i<3;i++)
SO[i]=bits[b][i];
SO[3]='\0';
return(SO);
}
char *SDES::SBOX1(char *l)
{
int S0[4][4]={0,1,2,3, 2,0,1,3,
3,0,1,0,
2,1,0,3
};
char *bits[]={"00","01","10","11"};
char lrow[3],lcol[3];
char *SO;
int i,lr,lc,b;
SO=new char[3];
lrow[0]=l[0];
lrow[1]=l[3];
lcol[0]=l[1];
lcol[1]=l[2];
lrow[2]='\0';
lcol[2]='\0';
for (i=0;i<4;i++)
{
if (strcmp(lrow,bits[i])==0)
lr=i;
if (strcmp(lcol,bits[i])==0)
lc=i;
}
b=S0[lr][lc];
for (i=0;i<3;i++)
SO[i]=bits[b][i];
SO[3]='\0';
return(SO);
}
void SDES::GetChar()
{
int i,j,in;
unsigned char ch=0;
char *bs;
bs=new char[9];
bs=InvIPOutput;
if (strlen(bs)>8)
{
printf("\nWRONG LENGTH STRING");
exit(0);
}
for (i=0;i<8;i++)
{
if (bs[i]=='1')
{
in=1;
for (j=1;j<8-i;j++)
{
in=in*2;
}
ch=ch+in;
}
}
OUTPUT=ch;
}
SDES::~SDES()
{
}
char *sfname,*tfname;
char *key;void main(void)
{
unsigned char ch,ch1;
int i,n=10,choice;
while (1)
{
key = new char[11];
sfname = new char[20];
tfname = new char[20];
mainmenu(&choice);
fflush(stdin);
switch (choice)
{
case 1:
menuEn();
DoEnDe(choice);
break;
case 2:
menuDe();
DoEnDe(choice);
break;
case 3:
exit(0);
default:
printf("\nWrong Choice Enter again\nPress any key to return to Main Menu..");
getch();
break;
}
}
}
void mainmenu(int *c)
{
printf("\nWhat do you want to do..");
printf("\n1. Encryption");
printf("\n2. Decryption");
printf("\n3. Exit");
printf("\n\nEnter the choice? ");
scanf("%d",c);
}
void menuEn()
{
sfname=new char[20];
tfname=new char[20];
key=new char[11];
printf("\nEncryption Menu\n\n");
printf("\nEnter the filename to be Encrypted: ");
gets(sfname);
printf("\nEnter the Target file name: ");
gets(tfname);
printf("\nEnter the 10-bits KEY: ");
gets(key);
printf("\n\nNotedown this key, as same key is used for Decryption");
}
void menuDe()
{
sfname=new char[20];
tfname=new char[20];
key=new char[11];
printf("\nDecryption Menu\n\n");
printf("\nEnter the filename to be Decrypted: ");
gets(sfname);
printf("\nEnter the Target file name: ");
gets(tfname);
printf("\nEnter the 10-bits KEY: ");
gets(key);
}
int DoEnDe(int c)
{
SDES S(key);
int i,n;
n=10; unsigned char ch;
FILE *fp;
FILE *ft;
fp=fopen(tfname,"w");
ft=fopen(sfname,"r");
if (fp==NULL)
{
printf("\nTarget File not opened SORRY");
getch();
fclose(fp);
return(0);
}
if (ft==NULL)
{
printf("\nSource File not opened SORRY");
getch();
fclose(ft);
return(0);
}
while (fread(&ch,1,1,ft)==1)
{
S.OUTPUT=ch;
for (i=0;i<n;i++)>
{
if (c==1)
S.DES_Encryption(S.OUTPUT);
if (c==2)
S.DES_Decryption(S.OUTPUT);
}
fwrite(&S.OUTPUT,1,1,fp);
}
printf("\nCompleted!!!!!");
getch();
fclose(fp);
fclose(ft);
return(1);
}