Você está na página 1de 6

Implementation of

Simple Data Encryption


Standard (SDES) in C++

Algorithm:
Step 1: Start the program.
Step 2: Declare the
necessary variables and
functions.
Step 3: Get the plain text
from the user
Step 4: Generate the key
for encrypting the plain
text.
Step 5: Encrypt the plain
text using the generated
key.
Step 6: Display the
encrypted text.
Step 7: Terminate the
program.

}
void xor(char *str1,char
*str2)
{

void convert_to_binary(int
num)
{

if(strlen(str1)!=strlen(str2))
{
cout<<"X-or can be done
with same length numbers
only!!!";
return;
}

if(num==0)
strcpy(result,"00");

for(int i=0;i<strlen(str1);i+
+)
{
if(str1[i]==str2[i])
result[i]='0';
else
result[i]='1';
}
result[i]=NULL;
}
int
convert_to_decimal(char
*str)
{
if(strlen(str)!=2)
return -1;

Source code:

if (strcmp(str,"00")==0)
return 0;

#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<math.h>

if (strcmp(str,"01")==0)
return 1;

char result[11];
char input_to_sbox[5];
char sbox0[4][4],sbox1[4]
[4];
char key1[9], key2[9],
key[10];

if (strcmp(str,"10")==0)
return 2;
if (strcmp(str,"11")==0)
return 3;
return -1;

if(num==1)
strcpy(result,"01");
if(num==2)
strcpy(result,"10");
if(num==3)
strcpy(result,"11");
}
void s_box(int s_box_no)
{
int row,column;
char temp1[3];
char buff[4];
strcpy(buff,input_to_sbox)
;
//strset(buff,'\0');
/*if(strlen(buff)!=4)
return "ERROR";*/
temp1[0]=input_to_sbox[0
];
temp1[1]=input_to_sbox[3
];
temp1[2]=NULL;
row=convert_to_decimal(t
emp1);
temp1[0]=input_to_sbox[1
];

temp1[1]=input_to_sbox[2
];
temp1[2]=NULL;
column=convert_to_decim
al(temp1);
//cout<<"Row:"<<row<<"
Columm"<<column;
int tmp;
if(s_box_no==0)
{
tmp=sbox0[row][column]48;
convert_to_binary(tmp);
//cout<<"SBox0:"<<result;
}
else
{
tmp=sbox1[row][column]48;
convert_to_binary(tmp);
//cout<<"SBox1:"<<result;
}
}

void fk(char *input,char*


key)
{
char temp[9],buff[2];
if(strlen(input)!=4 ||
strlen(key)!=8)
{
cout<<"Wrong input!!!";
return;
}
//Expansion and
Permutation E/P
temp[0]=temp[6]=input[3];
temp[1]=temp[7]=input[0];
temp[2]=temp[4]=input[1];

temp[3]=temp[5]=input[2];
temp[8]=NULL;
xor(temp,key); //Xor with
key...
strcpy(temp,result);
input_to_sbox[0]=temp[0];
input_to_sbox[1]=temp[1];
input_to_sbox[2]=temp[2];
input_to_sbox[3]=temp[3];
input_to_sbox[4]=NULL;
char str1[2],str2[2];
strset(result,'\0');
s_box(0);
result[2]='\0';
strcpy(str1,result);
input_to_sbox[0]=temp[4];
input_to_sbox[1]=temp[5];
input_to_sbox[2]=temp[6];
input_to_sbox[3]=temp[7];
input_to_sbox[4]=NULL;
strset(result,'\0');
s_box(1);
result[2]='\0';
strcat(str1,result);
result[0]=str1[1];
result[1]=str1[3];
result[2]=str1[2];
result[3]=str1[0];
result[4]=NULL;
//strcpy(result,s_box_result
);
}
void
generate_sub_keys(char
key[])
{

int
k1_order[]={0,6,8,3,7,2,9,
5};
int
k2_order[]={7,2,5,4,9,1,8,
0};
for(int i=1;i<=8;i++)
{
key1[i-1]=key[k1_order[i1]];
key2[i-1]=key[k2_order[i1]];
}
key1[8]=key2[8]=NULL;
}//void
generate_sub_keys()
void ip(char str[])
{
result[0]=str[1];
result[1]=str[5];
result[2]=str[2];
result[3]=str[0];
result[4]=str[3];
result[5]=str[7];
result[6]=str[4];
result[7]=str[6];
result[8]=NULL;
}
void ip_inverse(char str[])
{
result[0]=str[3];
result[1]=str[0];
result[2]=str[2];
result[3]=str[4];
result[4]=str[6];
result[5]=str[1];
result[6]=str[7];
result[7]=str[5];
result[8]=NULL;

}
int convert_to_dec(long int
bin)
{
int n=0,c=0,temp;
while(bin>0)
{
temp=bin%10;
bin/=10;
n+=pow(2,c)*temp;
c++;
}
return n;
}
void getachar(int ch,int
len=8)
{
long int
n,dec,temp,bin=0,c=0,coun
ter=0;
char str[10],tmp_str[10];
//cout<<"Enter a
Character:";
//dec=getche();
dec=ch;
while(dec>0)
{
temp=dec%2;
dec/=2;
bin+=pow10(c)*temp;
c++;
}
n=bin;
strset(tmp_str,'\0');
strset(str,'\0');
while(n>0)
{
temp=n%10;

n/=10;
tmp_str[counter]=temp+48
;
counter++;
///cout<<'\t'<<temp;
}
tmp_str[counter]=NULL;
strrev(tmp_str);

//strcpy(key,"1010101101"
);

for(int i=0;i<counter;i++)
str[i]='0';

menu:
clrscr();
cout<<"Enter Your
Option..."<<endl;
cout<<"Encrypt a File
1"<<endl;
cout<<"Decrypt a File
2"<<endl;
cout<<"Quit
3"<<endl;

str[i]=NULL;

choice=getch();

strcat(str,tmp_str);

switch(choice)
{

counter=len-counter;

strcpy(result,str);
}
void main()
{
char
in_file_path[50],out_file_p
ath[50];
char ch,choice;
char
plain_text[9],cipher_text[9
];
char l[5],r[5];
char buff[9];
char pass[25];
int i,tmp;
strcpy(sbox0[0],"1032");
strcpy(sbox0[1],"3210");
strcpy(sbox0[2],"0213");
strcpy(sbox0[3],"3132");
strcpy(sbox1[0],"0123");
strcpy(sbox1[1],"2013");
strcpy(sbox1[2],"3010");
strcpy(sbox1[3],"2103");

case '1':
cout<<"Enter the file to be
Encrypted:";
cin>>in_file_path;
cout<<"Enter a name for
the Encrypted file:";
cin>>out_file_path;
cout<<"Enter
Password(key):";
cin>>pass;
tmp=0;
cout<<endl<<"Pass:";
for(i=0;i<strlen(pass);i++)
{
cout<<pass[i];
tmp+=(int)pass[i];
}
tmp%=1024;
getachar(tmp,10);

strcpy(key,result);
ifstream
in_file(in_file_path);

if(!in_file)
{
cout<<"File not
Found!!!\a";
getch();
goto menu;
}
ofstream
out_file(out_file_path);
while(in_file.get(ch)!
=NULL)
{
//cout<<ch;
long int
n,dec,temp,bin=0,c=0,coun
ter=0;
char str[9],tmp_str[9];

n/=10;
tmp_str[counter]=temp+48
;
counter++;
///cout<<'\t'<<temp;
}
tmp_str[counter]=NULL;
strrev(tmp_str);
counter=8-counter;
for(i=0;i<counter;i++)
str[i]='0';
strcat(str,tmp_str);
strcpy(plain_text,str);
bin=0;
//cout<<endl<<"Plain
Text:"<<plain_text;
/*
cout<<"Enter The 8-bit
Plain Text:";
cin>>plain_text;
cout<<"Enter The 10-bit
Key:";
cin>>key;
*/

dec=ch;
while(abs(dec)>0)
{
temp=dec%2;
dec/=2;
bin+=pow10(c)*temp;
c++;
}
n=bin;
strset(tmp_str,'\0');
strset(str,'\0');
while(abs(n)>0)
{
temp=n%10;

//strcpy(key,"1100111001"
);
//********************
*****Start of
Encryption*************
*******************
//Generate Sub keys k1 and
k2...
generate_sub_keys(key);
// Initial Permutation...
ip(plain_text);
strcpy(plain_text,result);

l[0]=plain_text[0];
l[1]=plain_text[1];
l[2]=plain_text[2];
l[3]=plain_text[3];
l[4]=NULL;
r[0]=plain_text[4];
r[1]=plain_text[5];
r[2]=plain_text[6];
r[3]=plain_text[7];
r[4]=NULL;
fk(r,key1);//Fk1...
strcpy(buff,result);
xor(buff,l);// Xor with
left...
//Switch the blocks...
strcpy(buff,result);
strcpy(l,r);
strcpy(r,buff);
fk(r,key2);//Fk1...
strcpy(buff,result);
xor(buff,l); // Xor with
left...
strcpy(buff,result);
strcpy(l,buff);
strcpy(cipher_text,l);
strcat(cipher_text,r);
//cout<<"Cipher before IP1:"<<cipher_text;
ip_inverse(cipher_text);
strcpy(cipher_text,result);
//cout<<endl<<"Cipher
Text:"<<cipher_text;
bin=0;
for(i=7;i>=0;i--)
bin+=pow(10,7i)*((int)cipher_text[i]-48);

//cout<<endl<<bin;
dec=convert_to_dec(bin);
//cout<<(char)dec;
out_file.put(dec);
}//while(in_file.get(ch)!
=NULL)...
out_file.close();
break;
case '2':
cout<<"Enter the file to be
Decrypted:";
cin>>in_file_path;
cout<<"Enter a name for
the Decrypted file:";
cin>>out_file_path;

{
cout<<"File not
Found!!!\a";
getch();
goto menu;
}
ofstream
out_file_de(out_file_path);
long int dec=0,bin=0;
while(in_file_de.get(ch)!
=NULL)
{
//********************
*****Start of
Decryption*************
*******************
dec=ch;
if(dec<0)
dec+=256;
getachar(dec);

cout<<"Enter
Password(key):";
cin>>pass;

result[8]=NULL;
strcpy(cipher_text,result);
generate_sub_keys(key);
ip(cipher_text);
strcpy(cipher_text,result);

tmp=0;
cout<<endl<<"Pass:";
for(i=0;i<strlen(pass);i++)
{
cout<<pass[i];
tmp+=(int)pass[i];
}

l[0]=cipher_text[0];
l[1]=cipher_text[1];
l[2]=cipher_text[2];
l[3]=cipher_text[3];
l[4]=NULL;

tmp%=10241;
getachar(tmp,10);

r[0]=cipher_text[4];
r[1]=cipher_text[5];
r[2]=cipher_text[6];
r[3]=cipher_text[7];
r[4]=NULL;

strcpy(key,result);
ifstream
in_file_de(in_file_path);
if(!in_file_de)

fk(r,key2);//Fk1...
strcpy(buff,result);
xor(buff,l);// Xor with
left...

//Switch the blocks...


strcpy(buff,result);
strcpy(l,r);
strcpy(r,buff);
fk(r,key1);//Fk1...
strcpy(buff,result);
xor(buff,l); // Xor with
left...
strcpy(buff,result);
strcpy(l,buff);
strcpy(cipher_text,l);
strcat(cipher_text,r);
//cout<<"Cipher before IP1:"<<cipher_text;
ip_inverse(cipher_text);
strcpy(plain_text,result);
//cout<<endl<<"Plain
Text:"<<plain_text;
bin=0;
for(i=7;i>=0;i--)
bin+=pow(10,7i)*((int)plain_text[i]-48);
//cout<<endl<<bin;
dec=convert_to_dec(bin);
cout<<endl<<"The
Decrypted char
is:\'"<<(char)dec<<'\''<<en
dl<<"Its Ascii is:"<<dec;
out_file_de<<(char)dec;
//********************
*****End of
Decryption*************
*********************
}

out_file_de.close();
in_file_de.close();
break;

Do you want to use the


predetermined key(y/n)? n

case '3':
break;

Viva Questions:

default:
goto menu;
}
}
Output:
S-DES

Do you want to use the


predetermined key(y/n)? y
Please specify plain
text(10 BITS) : ben
PlainText = 00000000 [ +0
in decimal]
Key = 0000100000 [ +32
in decimal]
Encrypt
Cipher Text = 00101011
[+43 in decimal]
Decrypt
Decrypted Ciphertext =
00000000 [ +0 in decimal
S-DES

1. How many keys are


used for encryption
and decryption in
S-DES?
For the
encryption
of the plain
text in SDES two
keys are
used.
2. Define encryption
and decryption?
Encryption
is the
process for
converting
the plain
text into
hidden text
that cannot
be
understood
my others.
Decryption
is the
process of
converting
the cipher
text into
plain text.

3. Define symmetric
and asymmetric
encryption?
In
symmetric
encryption
the same
key is used
for both
encryption
and
decryption.
Whereas in
asymmetric
encryption
different
key is used
for
encryption
and
decryption.
4. What type of
encryption is SDES?
S-DES is a
symmetric
encryption.
5. What is the size of
the keys in SDES?
Two 8-bit
keys are
used for
encryption
&
decryption
in SDES.

Você também pode gostar