Upcoming SlideShare
×

# Microsoft Word Hw#1

943 views

Published on

Assignment # 1

Published in: Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
943
On SlideShare
0
From Embeds
0
Number of Embeds
35
Actions
Shares
0
20
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Microsoft Word Hw#1

1. 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
2. 2. 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()
3. 3. { 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;
4. 4. 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<<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: } //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; }
5. 5. 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)
6. 6. {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
7. 7. //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:
8. 8. 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
9. 9. { 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;
10. 10. 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)
11. 11. 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++)
12. 12. for(j=0;j<5;j++) matrix[i][j]=mktd[++m]; for(i=0;i<5;i++) {printf(quot;nquot;); for(j=0;j<5;j++) printf(quot;%cquot;,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; }