Monday 25 December 2017

Polyalphabetic Cipher (Vigenere Cipher) Technique.

Write a Program to perform Encryption & Decryption using Polyalphabetic
Cipher (Vigenere Cipher) Technique.



Code:


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
                    char p[20],c[26],key[20],d[26];
                    int i=0,j=0,l1=0,l2=0;
                    clrscr();
                    printf("\n Enter the plain text: ");
                    gets(p);
                    printf("\n Enter the key:------- ");
                    gets(key);
                    l1=strlen(p);
                    l2=strlen(key);
                    for(i=0;i<=l1-1;i++)
                    {
                              if(p[i]>=97&&p[i]<=122)
                              {
                                          c[i]=(((p[i]-97)+(key[j]-97))%26)+97;
                                          c[i+1]='\0';
                              }
                             else if(p[i]>=65&&p[i]<=90)
                             {
                                         c[i]=(((p[i]-65)+(key[j]-65))%26)+65;
                                         c[i+1]='\0';
                              }
                             else
                              {
                                       c[i]=p[i];
                                      c[i+1]='\0';
                              }
                             if(j<l2-1)
                             {
                                       j++;
                             }
                           else
                            {
                                       j=0;
                            }
                }
                j=0;
               for(i=0;i<=l1-1;i++)
              {
                             if(p[i]>=97&&p[i]<=122)
                             {
                                      d[i]=((((c[i]-97)-(key[j]-97))+26)%26)+97;
                                      d[i+1]='\0';
                              }
                              else if(p[i]>=65&&p[i]<=90)
                              {
                                       d[i]=((((c[i]-65)+(key[j]-65))+26)%26)+65;
                                       d[i+1]='\0';
                              }
                              else
                              {
                                      d[i]=c[i];
                                      d[i+1]='\0';
                              }
                              if(j<l2-1)
                              {
                                       j++;
                               }
                              else
                              {
                                       j=0;
                               }
              }
             printf("The encrypted word is :- ");
             puts(c);
             printf("\n The Decrypted word is :- ");
             puts(d);
             getch();
}

OUTPUT:



No comments:

Post a Comment