Connect to

facebook google Email you tube you tube you tube
Theme images by MichaelJay. Powered by Blogger.

Projects

7.3.17

Vigenere Cipher on C++


Before starting the explanation on Vigenere Cipher let us first know what is a Cipher.
So basically a Cipher is a secret or disguised way of writing a code.

Earlier we talked about Caesar Cipher and its implementation both in C and C++,  it was a kind of encryption where each letter was replaced by a letter corresponding to a certain number of alphabets after or before it.

Vigenere Cipher is a way of encryption where different series of  Caesar Cipher were used.
In simple words it was a type of poly-alphabetical encryption where a key decided by the person who encrypted the text, was used for decryption.
It also applied that a same alphabet in the same string can have a different code alphabet which made the decryption more and more complex.

Talking about the table which was used for encryption and decryption was basically a kind of mapping table.

So Looking at this table I ll give you an Example.
Just suppose "RAKSHIT" was to be encrypted and the key decided was "KEY"
So the key decided was written below the string this way :
                                         
Now Each alphabet was mapped so as to get the encrypted alphabet. For Example
R would be written as B when we consider the top row as out string and column as the key and so on for the rest of the string.
And so RAKSHIT would be encrypted as BEICLGD.

Now converting into computer logic i came across 2 formulas:
For Encryption E= (S1 + K1) % 26  (% means remainder)
For Decryption D= (E1 - K1 + 26) % 26  (% means remainder)


Now this Encryption assigns the alphabet in the form 0-25 but in C++ we have alphabets starting from the ASCII Value 65 and so in the source code I added 'A' to each alphabet after encryption.

Now Lets have the Source Code:

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<ctype.h>

//To repeat the Key till the
//string size and attain an new key
char* getkey2(char string[600],char key1[10])
 { char key2[600];
   strcpy(key2,key1);
   int i,x,y,ss,sk;
    ss=strlen(string);
    sk=strlen(key1);
     x=ss/sk;
     y=ss%sk;
    for(i=1;i<x;i++)
     strcat(key2,key1);
    x=strlen(key2);
     for(i=0;i<y;i++)
      { key2[x]=key1[i];
 x++;
      }
   return key2;
 }

//To Encrypt
char* encrypt(char string[600], char key2[600])
 { char em[600];
    for(int i=0; i<strlen(string); i++)
     { if(string[i]>=65 && string[i]<=90)
{ em[i] = ( string[i] + key2[i] ) % 26;
  em[i]+='A';
}
       else
 em[i]=string[i];
     }
    return em;
 }

//To Decrypt
char* decrypt(char string[600],char key2[600])
 { char dm[600];
    for(int i=0; i<strlen(string); i++)
     { if(string[i]>=65 && string[i]<=90)
 { dm[i] = ( string[i] - key2[i] + 26 ) % 26;
   dm[i]+='A';
 }
else
   dm[i]=string[i];
     }
  return dm;
 }


void main()
 { clrscr();
   char string[600],key1[10],key2[600],em[600],dm[600];
     cout<<"Enter Everything in Capital Letters.";
     cout<<"\nEnter Message to be Encrypted : ";gets(string);
     cout<<"\nEnter Key for Decryption(less than 10 alphabets)";gets(key1);


    strcpy(key2,getkey2(string,key1));
    strcpy(em,encrypt(string,key2));
    strcpy(dm,decrypt(em,key2));

    clrscr();
    cout<<"\nEncrypted Version : "<<em;
    cout<<"\nDecrypted Version : "<<dm;

    getch();
    }


Outputs : 



Drawbacks: 

  • It couldnt encrypt numbers
  • Everything had to be in Upper Case


No comments:
Write comments

Interested in our works and services?
Get more of our update !