The program accepts a 64-bit plaintext as a hexadecimal string, checks that it is valid hexadecimal input of exactly 16 characters, converts it to binary and arranges it in an 8x8 matrix. It then performs the initial permutation on the matrix, outputting the permuted values in binary format as Lo and Ro, and also converts them to hexadecimal output.
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. 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. 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