Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Microsoft Word Hw#2

1,012 views

Published on

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

Microsoft Word Hw#2

  1. 1. 1- Given an input key ABCDEF0123456789H, and the plaintext 0123456789ABCDEFH, perform a one round encryption using DES. Show intermediate results after each function. Solution: Calculations for the first subkey k1: - Input key: ABCDEF0123456789H (TABLE 1-1) Input key 64 bits 1 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1 1 1 1 0 1 1 1 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 1 0 1 0 0 0 1 0 1 0 1 1 0 0 1 1 1 1 0 0 0 1 0 0 1 (TABLE 1-2) Permuted Choice one (PC-1) [56 bits] 1 0 0 0 0 1 1 1 0 1 1 0 0 1 1 0 0 1 0 1 0 1 0 1 0 0 0 0 0 1 0 1 0 1 0 1 0 1 1 0 0 1 1 0 1 0 0 0 0 1 1 1 0 0 0 0 (TABLE 1-3) Left Shift by 1 (for round 1) for each 32 bits [C0 D0] separately 0 0 0 0 1 1 1 0 1 1 0 0 1 1 0 0 1 0 1 0 1 0 1 0 0 0 0 1 1 0 1 0 1 0 1 0 1 1 0 0 1 1 0 1 0 0 0 0 1 1 1 0 0 0 0 0
  2. 2. (TABLE 1-4)Permuted Choice Two (Pc-2) [48 bits Subkey K1] 1 1 0 0 0 1 0 1 0 1 1 1 1 1 0 0 0 0 0 1 0 0 1 0 1 0 1 1 0 0 0 0 1 0 1 0 1 1 0 0 0 0 0 1 1 0 1 0 Converting LAST table to Hexadecimal yields: C57C12B0AC1AH (TABLE 1-5) The plain Text: 0123456789ABCDEFH 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 1 0 1 0 0 0 1 0 1 0 1 1 0 0 1 1 1 1 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1 1 1 1 0 1 1 1 1 (TABLE 1-6) After Performing Initial Permutation (IP) 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 1 0 1 0 1 0 1 0 1 1 1 1 0 0 0 0 1 0 1 0 1 0 1 0 (TABLE 1-7) Expansion Permutation (E) for the 32 Right bits produced from the Initial permutation. 0 1 1 1 1 0 1 0 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 1 0 1 0 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1
  3. 3. (TABLE 1-8) (TABLE 1-4) XOR (TABLE 1-7) Number of Row in the S Boxes S1 1 0 1 1 1 1 S2 1 1 0 1 1 0 S3 1 0 0 1 0 1 S4 0 0 0 1 1 1 S5 1 1 0 0 1 0 S6 1 0 1 0 1 1 S7 1 0 0 1 0 1 S8 0 0 1 1 1 1 Number of Column in the S Box (TABLE 1-10) The result of substitution for the S-boxes 0 1 1 1 0 1 1 0 1 1 0 1 0 1 0 1 1 0 0 1 0 1 0 1 1 1 0 1 0 1 0 0 (TABLE 1-11) The result of permutation P for the last table 1 1 1 0 0 1 1 1 0 0 0 1 0 0 0 1 1 0 1 1 0 0 1 1 0 0 1 1 1 0 1 1 (TABLE 1-12) R1= (TABLE 1-11) XOR (32 lift bits TABLE 1-6) 0 0 1 0 1 0 1 1 0 0 0 1 0 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 0 1 0 0 (TABLE 1-13) OUTPUT OF ROUND 1 1 1 1 1 0 0 0 0 L1=R0 1 0 1 0 1 0 1 0 1 1 1 1 0 0 0 0 1 0 1 0 1 0 1 0 0 0 1 0 1 0 1 1 0 0 0 1 0 0 0 1 R1 0 1 1 1 1 1 1 1 1 1 0 0 0 1 0 0 IN HEXADECIMAL: L1 R1 : F0AAF0AA2B117FC4H
  4. 4. 2- For Des encryption: (all inputs are in hexadecimal, outputs are in both hexadecimal and binary) b- Write program that will accept 64bit plaintext and produces Lo and Ro. (the 32 bit each, left and right output of the initial permutation) The answer: The program works as follows: 1-It takes 64bits plaintext in hexadecimal format, that means it accepts 16 digits and only 16digits. 2-If the input data contained spaces it will be removed. 3-you can enter hexadecimal numbers like A or a whether uppercase or lowercase no difference. 4-In case you entered less or larger than 16 Hexadecimal digits. An appropriate message will appear asking the user to renter correct data. 5-In case you entered any character rather than any one of “0123456789abcdef”or “0123456789ABCDEF”, for example like “Q” then an appropriate message will appear asking the user to renter correct hexadecimal data. 6-The output of the program is: - The 8X8 binary matrix corresponding for the16 digits hexadecimal data entered. - The initial permutation IP 8X8 matrix.(i.e. Lo , Ro in Binary bits) - (Lo,Ro) in hexadecimal format. The Program: //Solution for problem 2-B #include <stdio.h> #include <ctype.h> #include<iostream.h> #include<iomanip.h> #include <stdlib.h> #include<math.h> #include<string.h> void arrange_input_plaintext_to_8_by_8_matrix(void); void initial_permutation(void); void conversion_to_hexadecimal_for_bin_permuted(void); void getting_plaintext_cheking_and_converting_to_decimal(void); int iptd[16],pt[8][8],ip[8][8]; int main(void) { getting_plaintext_cheking_and_converting_to_decimal(); arrange_input_plaintext_to_8_by_8_matrix(); initial_permutation(); return 0; } //This function gets the input plaintext in hexadecimal,checks it and rectifing it from spaces //moreover it converts it to decimal format void getting_plaintext_cheking_and_converting_to_decimal(void) { int i,t;
  5. 5. char s[1000]; cout<<quot;PLease enter the 64 bits plaintext in Hexadecimal format:quot;<<endl; xx: gets(s); for(i=0;i<strlen(s);i++) s[i]=tolower(s[i]); printf(quot;nquot;); t=-1; for(i=0;i<strlen(s);i++) { if(s[i]!=' ') { if((s[i]>=97)&&(s[i]<=102)) {iptd[++t]=s[i]-87; continue;} if((s[i]>=48)&&(s[i]<=57)) {iptd[++t]=s[i]-48; continue;} else {printf(quot;nERROR: Not all input data are in Hexadecimal format.nquot;); printf(quot;n Please renter 64bits plaintext in Hexadecimal format:nquot;); goto xx;} } if(s[i]==' ') continue; } if(t!=15) {cout<<quot;Error: Number of bits entered are not 64.quot;<<endl; cout<<quot; Please Renter correct 16 hexadecimal numbers:quot;<<endl; goto xx; } } //This function will take the input key text in hexadecimal and it will convert it to BINARY // //rearrange the data as 8*8 binary matrix //THE FUNCTION WILL PRINT out THE DESIREED ARRANGED MATRIX which is ready for the next stepquot;Permutationquot; void arrange_input_plaintext_to_8_by_8_matrix(void) { int i,j,n,x,b[4],cpt[16],onedpt[16][4],row,col,t; for(i=0;i<=15;i++) cpt[i]=iptd[i]; for(j=0;j<16;j++) {for(i=0;i<=3;i++) { x=cpt[j]%2; if(x==0) { onedpt[j][3-i]=0; cpt[j]/=2;}
  6. 6. else { onedpt[j][3-i]=1; cpt[j]=(cpt[j]-1)/2; } }} row=0;col=0; t=0; printf(quot;nThe arranged 8X8 binary matrix corresponding for the entered plain text is:nquot;); for(i=0;i<8;i++) { printf(quot;nquot;); for(j=0;j<8;j++) {pt[i][j]=onedpt[row][col++]; ++t; if(t%4==0) ++row; printf(quot;%dquot;,pt[i][j]); if(col>=4) col=0;} }} /*THIS FUNCTION WILL PERFORM THE INITIAL PERMUTATION FOR THE MATRIX WHICH RESULTED FROM THE AFOREMENTIONED FUNCTION*/ //ALSO IT WILL PRINT THE PERMUTED RESULT IN BOTH BINARY AND HEXADECIMAL void initial_permutation(void) { int i,j,t=-8,ipt[8][8]; for(i=0;i<8;i++) { for(j=0;j<8;j++) {if(i<=3) ip[i][j]=pt[7-j][2*i+1]; if(i>=4) ip[i][j]=pt[7-j][t]; } t+=2;} printf(quot;nThe initial permutaion 8X8 matrix is:nquot;); printf(quot;nLo:nquot;); for(i=0;i<8;i++) {printf(quot;nquot;); for(j=0;j<8;j++) {if((i==4)&&(j==0)) printf(quot;nRo:nquot;); printf(quot; %d quot;,ip[i][j]); } } conversion_to_hexadecimal_for_bin_permuted(); } //This Function converts the permuted values to hexadecimal and print them all out
  7. 7. void conversion_to_hexadecimal_for_bin_permuted(void) { int i,j,r=-1,odw[64],wdw[16][4],hex[64],sum; printf(quot;nquot;); for(i=0;i<8;i++) for(j=0;j<8;j++) odw[++r]=ip[i][j]; r=-1; printf(quot;nquot;); for(i=0;i<16;i++) for(j=0;j<4;j++) wdw[i][j]=odw[++r]; for(i=0;i<16;i++) { sum=0; for(j=0;j<4;j++) sum+=(pow(2,3-j))*wdw[i][j]; hex[i]=sum;} cout<<quot;In hexadecimal:quot;<<endl; cout<<quot; Lo quot;<<quot; Ro quot;<<endl; for(i=0;i<16;i++) { if(i==8) cout<<' '; printf(quot;%xquot;,hex[i]); } }

×