Solutions to Assignment #1
ECE 6106
Computer NW Security
By: Khalid El-Darymli
Matric. No.: G0327887
ECE Dept.
Faculty of Engineering
URL: eng.iiu.edu.my
International Islamic University Malaysia
a- Plain Text: “engineering excellence”
Encryption using Caesar’s cipher: HQJLQHHULQJ HAFHOOHQFH
b- Program for problem # 1:
//Program for problem# 1:
#include<iostream.h>
#include<string.h>
#include<stdio.h>
#define w 10000
#include<conio.h>
char b[w];
int chick(void);
void main()
{
void encrypt(void), decrypt(void);
char op;
cout<<quot;THIS PROGRAM CALCULATING THE ENCRYPTION OR DECRYPTION OF ENTERED
STRINGquot;;
cout<<quot; USING CAESAR's MOEHOD quot;<<endl;
cout<<quot;|**********************************************************************|quot;<<endl;
cout<<quot;Please Choose your CHOICEquot;<<endl;
cout<<quot;To ENCRYPT PLEASE PREES E >>>>quot;<<endl;
cout<<quot;To DECRYPT PLEASE PRESS D >>>quot;<<endl;
cout<<quot;TO EXIT PLEASE PRESS any other keyquot;<<endl;
cin>>op;
clrscr();
switch(op)
{
case 'e':case 'E': encrypt();break;
case 'd': case 'D': decrypt();break;
default: break;
}
}
/*THE FUNCTION OF ENCRYPTION */
void encrypt()
{
int i,s;
char *p,c[w],n;
cout<<quot;please enter the text to be encrypt:quot;<<endl;
aa:
gets(b);
if(chick()==1)
{cout<<quot;SORRY,ERROR IN THE INPUT DATA>>please RENTER the Ciphered text should be
alphabetic text quot;<<endl;
goto aa;}
for(i=0;i<=strlen(b);i++)
{
s=0;
p=&b[i];
if(*p==32||*p=='0')
{s=1;n=32;}
if(*p>=97&&*p<=119)
{s=1,n=*p%97+68;}
if(*p>=120&&*p<=122)
{s=1,n=*p%120+65;}
if(*p>=65&&*p<=87)
{s=1,n=*p+3;}
if(*p>=88&&*p<=90)
{s=1,n=*p%88+65;}
c[i]=n;
if(s==0)
{cout<<quot;YOU ENTERED NON-ALPHABETIC SYMBOL, ALL ENTERS SHOULD BE ALPHABETIC
LETTERSquot;<<endl;
goto xx;}
}
for(i=0;i<=strlen(b);i++)
cout<<c[i];
xx:
}
//THE FUNCTION OF MAKING DECRYPTION
void decrypt()
{
int i,s;
char *p,c[w],n;
cout<<quot;enter the text to be decryptquot;<<endl;
aa:
gets(b);
if(chick()==1)
{cout<<quot;SORRY,ERROR IN THE INPUT DATA>>please RENTER the Ciphered text should be
alphabetic text quot;<<endl;
3-Program for problem # 3:
//Program for problem# 3:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#define w 10000
char pt[w],kt[w],ct[w],ch[w];
char vignere_table_cipher(char,char);
char vignere_table_decipher(char,char);
int chick(void);
void main(){
char op;
void cipher(void);
void decipher(void);
cout<<quot;THIS PROGRAM PERFORMS ENCRYPTION OR DECRYPTION FOR ENTERED TEXT
usingquot;;
cout<<quot;Vigenere's autokey cipherquot;<<endl<<endl;
cout<<quot;|**********************************************************************|quot;<<endl;
cout<<quot;Please Choose your CHOICEquot;<<endl;
cout<<quot;[E] For ENCRYPTION please <<Press E followed by enter quot;<<endl;
cout<<quot;[D] For DECRYPTION please <<PRESS D followed by enterquot;<<endl;
cout<<quot; For EXIT please press any other key followed by enterquot;<<endl;
cin>>op;
switch(op)
{
case 'e': case 'E':cipher();break;
case 'd': case 'D':decipher();break;
default: break;
}}
//Ciphering function: This function performs encryption
void cipher(void)
{
int x,n,i;
char mkt[w],ct[w];
char *pp,*kp;
clrscr();
cout<<quot;Enter the text to be Encrypt:>>PLEASE REMOVE SPACES:quot;<<endl;
aa:
gets(pt);
strcpy(ch,pt);
if(chick()==1)
{cout<<quot;SORRY,ERROR IN THE INPUT DATA>>please RENTER just alphabetic text without
spacesquot;<<endl;
goto aa;}
cout<<quot;Enter the Encryption key:>>PLEASE REMOVE THE SPACES:quot;<<endl;
strcpy(ch,quot; quot;);
bb:
gets(kt);
strcpy(ch,kt);
if(chick()==1)
{cout<<quot;SORRY,ERROR IN THE INPUT DATA>>please RENTER just alphabetic text without
spacesquot;<<endl;
goto bb;}
for (i=0; i<strlen(pt); i++)
pt[i] = tolower(pt[i]);
for(i=0;i<strlen(kt);i++)
kt[i]=tolower(kt[i]);
//Making the length of key equal to the length of plain text
x=strlen(pt);
xx:
x-=strlen(kt);
if(x>=0)
{n=strlen(kt);
strncat(mkt,kt,n);
goto xx; }
else
strncat(mkt,kt,x+strlen(kt));
// end of process
//Ciphering and printing of the plaintext
for(i=0;i<strlen(pt);i++)
{
pp=&pt[i];
kp=&mkt[i];
ct[i]=vignere_table_cipher(*pp,*kp);
}
printf(quot;nquot;);
for(i=0;i<strlen(ct);i++)
ct[i]=toupper(ct[i]);
puts(ct);
}
//End of process
//This function calculating the character corresponding to that we obtain fro Vignere Table
//for two submitted character, the key character and the plain text text character
//the function returns the ciphered character
char vignere_table_cipher(char k,char p)
{
char c;
int d,comp;
comp=k-'a';
d=p+comp;
if(d>122)
{d%=122;
d+=96;}
c=d;
return c;
}
//Function decipher, this function perofoms decryption.
void decipher(void)
{
int x,n,i;
char mkt[w],dt[w];
char *pp,*kp,rvv;
clrscr();
cout<<quot;Enter the text to be DECRYPT:>>PLEASE remove the spaces>>quot;<<endl;
aa:
gets(ct);
strcpy(ch,ct);
if(chick()==1)
{cout<<quot;SORRY,ERROR IN THE INPUT DATA>>please RENTER the Ciphered text just alphabetic
text without spacesquot;<<endl;
goto aa;}
strcpy(ch,quot; quot;);
cout<<quot;Enter the DECRYPTION key:>>PLEASE REMOVE THE SPACES>>quot;<<endl;
bb:
gets(kt);
strcpy(ch,kt);
if(chick()==1)
{cout<<quot;SORRY,ERROR IN THE INPUT DATA>>please RENTER the KEY just as alphabetic text
without spacesquot;<<endl;
goto bb;}
for (i=0; i<strlen(ct); i++)
ct[i] = tolower(ct[i]);
for(i=0;i<strlen(kt);i++)
kt[i]=tolower(kt[i]);
//Making the length of key equal to the length of cipheered text
x=strlen(ct);
xx:
x-=strlen(kt);
if(x>=0)
{n=strlen(kt);
strncat(mkt,kt,n);
goto xx; }
else
strncat(mkt,kt,x+strlen(kt));
// end of process
//deCiphering and printing of the plaintext
for(i=0;i<strlen(ct);i++)
{
pp=&ct[i];
kp=&mkt[i];
dt[i]=vignere_table_decipher(*kp,*pp);
}
printf(quot;nquot;);
puts(dt);
}
//End of process
//This function calculating the character corresponding to that we obtain from Vignere Table
//for two submitted character, the key character and the ciphered text character
//the function returns the plain text character
char vignere_table_decipher(char kt,char ct)
{
char d;
int comp,pt;
comp=kt-'a';
pt=ct-comp;
if(pt<97)
pt+=123-97;
d=pt;
return d;
}
//End of function
//Function chick, this function chicks the input data to make sure they are
//pure alphabetics and without spaces
int chick(void)
{ int test,i;
for(i=0;i<strlen(ch);i++)
if((ch[i]>=65&&ch[i]<=90)||(ch[i]>=97&&ch[i]<=122))
test=0;
else
{
test=1;
return test;
}
return test;
}
2-
a- Plain Text: “Engineering Excellence”
Key: “Kulliyyah”
Encryption Using Playfair: FEMLEFMOMYMFIMGKKGMDE
Note: Sorry, this program not ready yet. It’s just admits the entered key text and mixes it with
the alphabets then it forms 5X5 matrix.
#include<stdio.h>
#include<string.h>
#include<iostream.h>
#include<stdlib.h>
#include<ctype.h>
#define w 999
void distributing_of_array(void);
void remove_of_repetation(void);
void removing_of_i_or_j(void);
void converting_to_5_by_5_matrix(void);
int chick(void);
char kt[w],ktd[w],mktd[w],matrix[5][5],ch[w];
main()
{
cout<<quot;Enter the Encryption key:>>PLEASE REMOVE THE SPACES:quot;<<endl;
strcpy(ch,quot; quot;);
bb:
gets(kt);
strcpy(ch,kt);
if(chick()==1)
{cout<<quot;SORRY,ERROR IN THE INPUT DATA>>please RENTER just alphabetic key text without
spacesquot;<<endl;
mktd[++k]=ktd[i];
}
}
void removing_of_i_or_j(void)
{
int p1,p2,i,p;
p1=0;
p2=0;
for(i=0;i<strlen(mktd);i++)
{
if(mktd[i]=='i')
p1=i+1;
if(mktd[i]=='j')
p2=i+1;
}
if((p1!=0)&&(p2!=0))
{
if(p1>p2)
mktd[p1-1]='j';
if(p2>p1)
mktd[p2-1]='i';}
for(i=0;i<26;i++)
ktd[i]=mktd[i];
remove_of_repetation();
}
//THIS FUNCTION COMBINE THE KEY TEXT WITH THE ALPHABETICS.
//NOTE if you entered key text contains 'i' then the letter 'j' won't appear
//and vica versa
//again if you entered text contins i and j appeared respictively then j will be removed and vica
versa
void converting_to_5_by_5_matrix(void)
{
int i,j,m=-1;
for(i=0;i<5;i++)