Académique Documents
Professionnel Documents
Culture Documents
The cipher text alphabet for the cipher is where you replace each letter by the next letter in
the alphabet.
PROGRAM:
#include<stdio.h>
int main()
{
char *message,*emessage,*dmessage;
int i,j=0,k,key,temp;
clrscr();
printf("\nEnter the key\n");
scanf("%d",&key);
key=key%26;
printf("\nEnter message\n");
fflush(stdin);
gets(message);
for(i=0;message[i]!=NULL;i++)
message[i]=tolower(message[i]);
Made by-Indu
Page 1
for(i=0;message[i]!=NULL;i++)
{
//printf("%c ",message[i]);
if(message[i]==' ')
emessage[j++]=message[i];
else
{
if(message[i]>=48 && message[i]<=57)
{
temp=message[i]+key;
if(temp>57)
emessage[j++]=48+(temp-58);
else
emessage[j++]=temp;
}
else
{
if(message[i]>=97 && message[i]<=123)
{
temp=message[i]+key;
if(temp>122)
emessage[j++]=97+(temp-123);
else
emessage[j++]=temp;
}
else
emessage[j++]=message[i];
}
// printf("%c ",emessage[j]);
}
}
emessage[j]='\0';
printf("\n\n\nEncrypted message is\n\n");
for(i=0;emessage[i]!=NULL;i++)
printf("%c",emessage[i]);
// printf("\n end");
for(i=0,j=0;emessage[i]!=NULL;i++)
{
if(emessage[i]==' ')
dmessage[j++]=emessage[i];
else
{
if(emessage[i]>=48 && emessage[i]<=57)
{
temp=emessage[i]-key;
Made by-Indu
Page 2
if(temp<48)
dmessage[j++]=58-(48-temp);
else
dmessage[j++]=temp;
}
else
{
if(emessage[i]>=97 && emessage[i]<=123)
{
temp=emessage[i]-key;
if(temp<97)
dmessage[j++]=123-(97-temp);
else
dmessage[j++]=temp;
}
else
dmessage[j++]=emessage[i];
}
}
}
dmessage[j]='\0';
printf("\n\n\nRetrieved message is\n\n");
for(i=0;dmessage[i]!=NULL;i++)
printf("%c",dmessage[i]);
getch();
return(0);
}
Made by-Indu
Page 3
EXPERIMENT 2
Aim: WAP to encrypt and decrypt a message using playfair cipher.
In this scheme, pairs of letters are encrypted, instead of single letters as in the case of simple
substitution cipher.
In playfair cipher, initially a key table is created. The key table is a 55 grid of alphabets
that acts as the key for encrypting the plaintext. Each of the 25 alphabets must be unique and
one letter of the alphabet (usually J) is omitted from the table as we need only 25 alphabets
instead of 26. If the plaintext contains J, then it is replaced by I.
The sender and the receiver deicide on a particular key, say tutorials. In a key table, the
first characters (going left to right) in the table is the phrase, excluding the duplicate letters.
The rest of the table will be filled with the remaining letters of the alphabet, in natural order.
The key table works out to be
PROGRAM
#include<stdio.h>
#include<conio.h>
int check(char table[5][5],char k)
{
int i,j;
for(i=0;i<5;++i)
for(j=0;j<5;++j)
{
if(table[i][j]==k)
return 0;
}
return 1;
Made by-Indu
Page 4
}
void main()
{
int i,j,key_len,flag,count=0, val=97,l=0,length=0,count1=0,r1,r2,c1,c2,k1;
char p[50],table[5][5],key[50],p1[50],temp1,bogus,cipher_text[50];
clrscr();
for(i=0;i<5;++i)
for(j=0;j<5;++j)
table[i][j]='0';
printf("**********Playfair Cipher************\n\n");
printf("Enter the length of the Key. ");
scanf("%d",&key_len);
printf("Enter the Key. ");
for(i=-1;i<key_len;++i)
{
scanf("%c",&key[i]);
if(key[i]=='j')
key[i]='i';
}
// inserting the key into the table
for(i=0;i<5;++i)
{
for(j=0;j<5;++j)
{
flag=0;
while(flag!=1)
{
if(count>key_len)
goto l1;
flag=check(table,key[count]);
++count;
}// end of while
table[i][j]=key[(count-1)];
}// end of inner for
}// end of outer for
l1:printf("\n");
//inserting other alphabets
for(i=0;i<5;++i)
{
for(j=0;j<5;++j)
{
if(table[i][j]>=97 && table[i][j]<=123)
{}
else
{
Made by-Indu
Page 5
flag=0;
while(flag!=1)
{
if('j'==(char)val)
++val;
flag=check(table,(char)val);
++val;
}// end of while
table[i][j]=(char)(val-1);
}//end of else
}// end of inner for
}// end of outer for
printf("The table is as follows:\n");
for(i=0;i<5;++i)
{
for(j=0;j<5;++j)
{
printf("%c ",table[i][j]);
}
printf("\n");
}
printf("\nEnter the length length of plain text.(without spaces) ");
scanf("%d",&l);
printf("\nEnter the Plain text. ");
for(i=-1;i<l;++i)
{
scanf("%c",&p[i]);
}
for(i=-1;i<l;++i)
{
if(p[i]=='j')
p[i]='i';
}
printf("\nThe replaced text(j with i)");
for(i=-1;i<l;++i)
printf("%c ",p[i]);
count=0;
for(i=-1;i<l;++i)
{
if(p[i]==p[i+1])
count=count+1;
}
printf("\nThe cipher has to enter %d bogus char.It is either 'x' or 'z'\n",count);
Made by-Indu
Page 6
if((l+count)%2!=0)
length=(l+count+1);
else
length=(l+count);
printf("\nValue of length is %d.\n",length);
//inserting bogus characters.
for(i=-1;i<l;++i)
{
p1[count1]=p[i];
if(p[i]==p[i+1])
{
count1=count1+1;
if(p[i]=='x')
p1[count1]='z';
else
p1[count1]='x';
}
count1=count1+1;
}
//checking for length
if((l+count)%2!=0)
{
if(p1[length-1]=='x')
p1[length]='z';
else
p1[length]='x';
}
printf("The final text is:");
for(i=0;i<=length;++i)
printf("%c ",p1[i]);
for(k1=1;k1<=length;++k1)
{
for(i=0;i<5;++i)
{
for(j=0;j<5;++j)
{
if(table[i][j]==p1[k1])
{
r1=i;
c1=j;
}
else
if(table[i][j]==p1[k1+1])
{
r2=i;
c2=j;
}
Made by-Indu
Page 7
Made by-Indu
Page 8
Made by-Indu
Page 9