Description
Vigenere cipher is
used for encrypting alphabetic text. In a Caesar cipher, each letter of the alphabet is
shifted along some number of places; for example, in a Caesar cipher of shift
3, A would become D, Bwould become E, Y would become B and so on. The
Vigenère cipher consists of several Caesar ciphers in sequence with different
shift values.
To encrypt, a table of alphabets can be used, termed as Vigenère
square, or Vigenère table. It consists of the alphabet written
out 26 times in different rows, each alphabet shifted cyclically to the left
compared to the previous alphabet, corresponding to the 26 possible Caesar
ciphers. At different points in the encryption process, the cipher uses a
different alphabet from one of the rows. The alphabet used at each point
depends on a repeating key.
For example, suppose that the plaintext to be encrypted is:
cryptograph
The person sending the message chooses a keyword and repeats it
until it matches the length of the plaintext, for example, the keyword
"vein":
veinveinvei
Each row starts with a key letter. The remainder of the row holds
the letters A to Z (in shifted order). Although there are 26 key rows shown,
you will only use as many keys (different alphabets) as there are unique letters
in the key string, here just 4 keys, {v,e,i,n}. For successive letters of the
message, we are going to take successive letters of the key string, and
encipher each message letter using its corresponding key row. Choose the next
letter of the key, go along that row to find the column heading that matches
the message character; the letter at the intersection of row and column, is the enciphered letter.
For example, the first letter of the plaintext, c, is paired with v, the first letter of
the key. So use row v and column c of the Vigenère
square, namely v. Similarly, for the second letter of the
plaintext, the second letter of the key is used; the letter at row r and column e is v. The rest of the
plaintext is enciphered in a similar fashion:
Plaintext:
|
cryptograph
|
Key:
|
veinveinvei
|
Ciphertext:
|
Xvgcojoevtb
|
Decryption is performed by going to the row in the table
corresponding to the key, finding the position of the ciphertext letter in this
row, and then using the column's label as the plaintext. For example, in row v (from vein),
the ciphertext v appears in column c, which is the first
plaintext letter. Next we go to row e (from vein), locate the ciphertext v which is found in
column r, thus r is the second plaintext letter.
Implementation in c++
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main(){
clrscr();
char
*ptext,*cih,*key,alp[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
int padd[50],kadd[50],padd1[50],kadd1[50],keyval,pval,i;
cout<<"\nEnter Plain text:";
cin>>ptext;
cout<<"\nEnter Key:";
cin>>key;
pval=strlen(ptext);
keyval=strlen(key);
for(i=0;i<pval;i++){
key[i]=key[i%keyval];
}
int count=0,count1=0;
while(count<pval){
for(i=0;i<26;i++){
if(ptext[count]==alp[i]){
padd[count]=i;
for(int j=0;j<26;j++){
if(key[count]==alp[j]){
padd[count]=padd[count]+j;
}
}
}
}
count++;
}
for(i=0;i<pval;i++){
kadd[i]=padd[i]%26;
}
cout<<"\nCipher Text will be:";
for(i=0;i<pval;i++){
cih[i]=alp[kadd[i]];
cout<<alp[kadd[i]];
}
while(count1<pval){
for(i=0;i<26;i++){
if(cih[count1]==alp[i]){
padd1[count1]=i;
for(int j=0;j<26;j++){
if(key[count1]==alp[j]){
padd1[count1]=padd1[count1]-j;
}
}
}
}
count1++;
}
for(i=0;i<pval;i++){
kadd1[i]=padd1[i]%26;
if(kadd1[i]<0){
kadd1[i]+=26;
}
}
cout<<"\nOriginal Text will be:";
for(i=0;i<pval;i++){
cout<<alp[kadd1[i]];
}
getch();
}
Output
Enter Plain text: cryptograph
Enter key: vein
Cipher Text will be: Xvgcojoevtb