Professional Documents
Culture Documents
Section-2 Crypto Lab
Section-2 Crypto Lab
Then
perform
the reverse operation to get original plaintext.
(In C)
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
char *transposition(char*) ;
char *revTransposition(char*);
int n,hold,width;
main ({)
n=strlen(plainText);
Lf (width>=n)
stropy (cipherText,plainText);
strcepy (revPlainText,plainText);
strcpy (revPlainText?,plainText) ;
else
return OQ;
int i,j7
char *“cipherText;
j=0;
hold=-1;
while (j<n)
{
for (i=++hold;i<n;it=width)
cipherText [j++]=plainText [i];
}
cipherText[n]J="\0';
réturn cipherText;
}
char *revTransposition(char® plainText}
{
int i,j?
char *cipherText;
3=0;
hold=-1;
while (j<n)
{
for (i=++hold;i<n;i+t=width)
cipherText [i]=plainText [j++];
}
cipherText[n]="\0';
return ciphertext;
}
2. To encrypt a given message using ONE TIME PAD technique. Then perform the reverse
operation to set original plaintext,
(In C)
#include<stdio.h>
#include<conic.h>
#include<string.h>
#finclude<ctype.h>
main()
{
int i,n;
char *plainText, *cipherText, *revPlainText;
char *oneTimePadUpper="ABCDEFGHIJKLMNOPORSTUVWKYE";
char *oneTimePadLower="abcdefghijkimnoparstuvwxyz";
char tmp;
printi(™\nEnter the plaintext (Alphabet onlyj:");
gets (plaintext);
n=strlen(plainText);
for (i=O;i<n;it++t)
{
if {islower (plainText[i]))
{
tmp=plainText [i] +oneTimePadLower [i]-192;
cipherText [i]=96+ (tmp%26) ;
}
else if(isupper (plainText[i]})
{
tmp=plainText [i] toneTimePadUpper [i]-128;
cipherText [i]=64+ (tmp%26) ;
}
else cipherText[i]=plainText[il];
}
cipherText[n]="\O';
printfi("\nAfter using one time pad: %s",cipherText);
getch();
For (i=O;i<n;it+)
{
if (islower (cipherText[ij]))
{
tmp=96+cipherText [i] -oneTimerPadLower [il];
if(tmp<37) revPlainText [i)=tmp+26;
else revPlainText[i)=tmp;
}
else if(lsupper (cipherText[1i]})
{
tmp=64+cipherText [i] -oneTimePadUpper[il;
if({tmp<65) revPlainText [i)=tmp+26;
else revPlainText[i]=tmp;
}
else revPlainText [i] =cipherText [i];
}
revPlainText(n]="\0';
printf("\nAfter reverse original text is: %s",revwPlainText);
getch();
return 0;
}