Vigenere Cipher Implementation > C Program

Vigenere Cipher Implementation > C Program

Cryptography and System Security


Program:

#include<stdio.h>


void main()
{
int i;
int kl;
int pl;
printf("Enter the length of the key stream. ");
scanf("%d",&kl);
printf("Enter the length of the plain text stream.(Without spaces) ");
scanf("%d",&pl);

char p[pl];
char k[kl];

printf("\nEnter the Key. ");
for(i=-1;i<kl;++i)
scanf("%c",&k[i]);

printf("\nEnter the Plain text. ");
for(i=-1;i<pl;++i)
scanf("%c",&p[i]);

int s[3][pl];

for(i=0;i<pl;++i)
{
if(65<=p[i] && p[i]<=91)
s[0][i]=p[i]%65;
else
s[0][i]=p[i]%97;
}

for(i=0;i<pl;++i)
printf("%d ",s[0][i]);
int count=0;
while(count<pl)
{
for(i=0;i<kl;++i)
{
if(65<=k[i] && k[i]<=91)
s[1][count+i]=k[i]%65;
else
s[1][count+i]=k[i]%97;
}
count=count+kl;
}
printf("\n");
for(i=0;i<pl;++i)
printf("%d ",s[1][i]);

printf("\n");
for(i=0;i<pl;++i)
printf("%d ",s[2][i]);

printf("\n\nThe cipher text is: ");
char cipher[kl];
for(i=0;i<pl;++i)
{
s[2][i]=(s[0][i]+s[1][i])%26;
cipher[i]=(char)(s[2][i]+65);
printf("%c ",cipher[i]);
}

}

/*Output

[prat2@localhost Desktop]$ gcc vignere.c
[prat2@localhost Desktop]$ ./a.out
Enter the length of the key stream. 6
Enter the length of the plain text stream.(Without spaces) 14

Enter the Key. pascal

Enter the Plain text. sheislistening

18 7 4 8 18 11 8 18 19 4 13 8 13 6 
15 0 18 2 0 11 15 0 18 2 0 11 15 0 

The cipher text is: H H W K S W X S L G N T C G 
*/

Comments

Popular posts from this blog

Intermediate Code Generation > C Program