Microsoft Word Hw#1

12
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

description

Assignment # 1

Transcript of Microsoft Word Hw#1

Page 1: Microsoft Word   Hw#1

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

Page 2: Microsoft Word   Hw#1

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<<"THIS PROGRAM CALCULATING THE ENCRYPTION OR DECRYPTION OF ENTERED STRING"; cout<<" USING CAESAR's MOEHOD "<<endl; cout<<"|**********************************************************************|"<<endl; cout<<"Please Choose your CHOICE"<<endl; cout<<"To ENCRYPT PLEASE PREES E >>>>"<<endl; cout<<"To DECRYPT PLEASE PRESS D >>>"<<endl; cout<<"TO EXIT PLEASE PRESS any other key"<<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()

Page 3: Microsoft Word   Hw#1

{ int i,s; char *p,c[w],n; cout<<"please enter the text to be encrypt:"<<endl; aa: gets(b); if(chick()==1) {cout<<"SORRY,ERROR IN THE INPUT DATA>>please RENTER the Ciphered text should be alphabetic text "<<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<<"YOU ENTERED NON-ALPHABETIC SYMBOL, ALL ENTERS SHOULD BE ALPHABETIC LETTERS"<<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<<"enter the text to be decrypt"<<endl; aa: gets(b); if(chick()==1) {cout<<"SORRY,ERROR IN THE INPUT DATA>>please RENTER the Ciphered text should be alphabetic text "<<endl;

Page 4: Microsoft Word   Hw#1

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>=100&&*p<=122) {s=1,n=*p-3;} if(*p>=97&&*p<=99) {s=1,n=120-*p%97;} if(*p>=68&&*p<=90) {s=1,n=*p%68+97;} if(*p>=65&&*p<=67) {s=1,n=120-*p%65;} c[i]=n; if(s==0) {cout<<"YOU ENTERED NON-ALPHABETIC SYMBOL, ALL ENTERS SHOULD BE ALPHABETIC LETTERS"<<endl; goto xx;} } for(i=0;i<=strlen(b);i++) cout<<c[i]; xx: } //Function chick, this function chicks the input data to make sure THAT they are //pure alphabetics WITH OR WITHOUT SPACES int chick(void) { int test,i; for(i=0;i<strlen(b);i++) if((b[i]>=65&&b[i]<=90)||(b[i]>=97&&b[i]<=122)||(b[i]==32)) test=0; else { test=1; return test; } return test; }

Page 5: Microsoft Word   Hw#1

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<<"THIS PROGRAM PERFORMS ENCRYPTION OR DECRYPTION FOR ENTERED TEXT using"; cout<<"Vigenere's autokey cipher"<<endl<<endl; cout<<"|**********************************************************************|"<<endl; cout<<"Please Choose your CHOICE"<<endl; cout<<"[E] For ENCRYPTION please <<Press E followed by enter "<<endl; cout<<"[D] For DECRYPTION please <<PRESS D followed by enter"<<endl; cout<<" For EXIT please press any other key followed by enter"<<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<<"Enter the text to be Encrypt:>>PLEASE REMOVE SPACES:"<<endl; aa: gets(pt); strcpy(ch,pt); if(chick()==1)

Page 6: Microsoft Word   Hw#1

{cout<<"SORRY,ERROR IN THE INPUT DATA>>please RENTER just alphabetic text without spaces"<<endl; goto aa;} cout<<"Enter the Encryption key:>>PLEASE REMOVE THE SPACES:"<<endl; strcpy(ch," "); bb: gets(kt); strcpy(ch,kt); if(chick()==1) {cout<<"SORRY,ERROR IN THE INPUT DATA>>please RENTER just alphabetic text without spaces"<<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("\n"); 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

Page 7: Microsoft Word   Hw#1

//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<<"Enter the text to be DECRYPT:>>PLEASE remove the spaces>>"<<endl; aa: gets(ct); strcpy(ch,ct); if(chick()==1) {cout<<"SORRY,ERROR IN THE INPUT DATA>>please RENTER the Ciphered text just alphabetic text without spaces"<<endl; goto aa;} strcpy(ch," "); cout<<"Enter the DECRYPTION key:>>PLEASE REMOVE THE SPACES>>"<<endl; bb: gets(kt); strcpy(ch,kt); if(chick()==1) {cout<<"SORRY,ERROR IN THE INPUT DATA>>please RENTER the KEY just as alphabetic text without spaces"<<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:

Page 8: Microsoft Word   Hw#1

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("\n"); 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

Page 9: Microsoft Word   Hw#1

{ 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<<"Enter the Encryption key:>>PLEASE REMOVE THE SPACES:"<<endl; strcpy(ch," "); bb: gets(kt); strcpy(ch,kt); if(chick()==1) {cout<<"SORRY,ERROR IN THE INPUT DATA>>please RENTER just alphabetic key text without spaces"<<endl;

Page 10: Microsoft Word   Hw#1

goto bb;} distributing_of_array(); puts(mktd); removing_of_i_or_j(); converting_to_5_by_5_matrix(); } void distributing_of_array() { int i; char a[26]; //to ensure all enters are lower case letters for(i=0;i<strlen(kt);i++) kt[i]=tolower(kt[i]); strcat(ktd,kt); for(i=0;i<26;i++) a[i]='a'+i; strcat(ktd,a); remove_of_repetation(); } void remove_of_repetation(void) { mktd[0]=ktd[0]; int i,j,k=0,u,s=0; for(i=1;i<strlen(ktd);i++) { s=0; for(j=0;j<k+1;j++) if(ktd[i]==mktd[j]) s=1; if(s==0)

Page 11: Microsoft Word   Hw#1

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++)

Page 12: Microsoft Word   Hw#1

for(j=0;j<5;j++) matrix[i][j]=mktd[++m]; for(i=0;i<5;i++) {printf("\n"); for(j=0;j<5;j++) printf("%c",matrix[i][j]); } } //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; }