In this tutorial you will learn about vigenere cipher in C and C++ for encryption and decryption.
Vigenere Cipher is kind of polyalphabetic substitution method. It is used for encryption of alphabetic text. For encryption and decryption Vigenere Cipher Table is used in which alphabets from A to Z are written in 26 rows.
Also Read: Caesar Cipher in C and C++ [Encryption & Decryption]
Also Read: Hill Cipher in C and C++ (Encryption and Decryption)
Vigenere Cipher Encryption
Message Text: THECRAZYPROGRAMMER
Key: HELLO
Here we have to obtain a new key by repeating the given key till its length become equal to original message length.
New Generated Key: HELLOHELLOHELLOHEL
For encryption take first letter of message and new key i.e. T and H. Take the alphabet in Vigenere Cipher Table where T row and H column coincides i.e. A.
Repeat the same process for all remaining alphabets in message text. Finally the encrypted message text is:
Encrypted Message: ALPNFHDJAFVKCLATIC
The algorithm can be expressed in algebraic form as given below. The cipher text can be generated by below equation.
Ei = (Pi + Ki) mod 26
Here P is plain text and K is key.
Vigenere Cipher Decryption
Encrypted Message: ALPNFHDJAFVKCLATIC
Key: HELLO
New Generated Key: HELLOHELLOHELLOHEL
Take first alphabet of encrypted message and generated key i.e. A and H. Analyze Vigenere Cipher Table, look for alphabet A in column H, the corresponding row will be the first alphabet of original message i.e. T.
Repeat the same process for all the alphabets in encrypted message.
Original Message: THECRAZYPROGRAMMER
Above process can be represented in algebraic form by following equation.
Pi = (Ei – Ki + 26) mod 26
We will use above algebraic equations in the program.
Program for Vigenere Cipher in C
#include<stdio.h> #include<string.h> int main(){ char msg[] = "THECRAZYPROGRAMMER"; char key[] = "HELLO"; int msgLen = strlen(msg), keyLen = strlen(key), i, j; char newKey[msgLen], encryptedMsg[msgLen], decryptedMsg[msgLen]; //generating new key for(i = 0, j = 0; i < msgLen; ++i, ++j){ if(j == keyLen) j = 0; newKey[i] = key[j]; } newKey[i] = '\0'; //encryption for(i = 0; i < msgLen; ++i) encryptedMsg[i] = ((msg[i] + newKey[i]) % 26) + 'A'; encryptedMsg[i] = '\0'; //decryption for(i = 0; i < msgLen; ++i) decryptedMsg[i] = (((encryptedMsg[i] - newKey[i]) + 26) % 26) + 'A'; decryptedMsg[i] = '\0'; printf("Original Message: %s", msg); printf("\nKey: %s", key); printf("\nNew Generated Key: %s", newKey); printf("\nEncrypted Message: %s", encryptedMsg); printf("\nDecrypted Message: %s", decryptedMsg); return 0; }
Output
Original Message: THECRAZYPROGRAMMER
Key: HELLO
New Generated Key: HELLOHELLOHELLOHEL
Encrypted Message: ALPNFHDJAFVKCLATIC
Decrypted Message: THECRAZYPROGRAMMER
Program for Vigenere Cipher in C++
#include<iostream> #include<string.h> using namespace std; int main(){ char msg[] = "THECRAZYPROGRAMMER"; char key[] = "HELLO"; int msgLen = strlen(msg), keyLen = strlen(key), i, j; char newKey[msgLen], encryptedMsg[msgLen], decryptedMsg[msgLen]; //generating new key for(i = 0, j = 0; i < msgLen; ++i, ++j){ if(j == keyLen) j = 0; newKey[i] = key[j]; } newKey[i] = '\0'; //encryption for(i = 0; i < msgLen; ++i) encryptedMsg[i] = ((msg[i] + newKey[i]) % 26) + 'A'; encryptedMsg[i] = '\0'; //decryption for(i = 0; i < msgLen; ++i) decryptedMsg[i] = (((encryptedMsg[i] - newKey[i]) + 26) % 26) + 'A'; decryptedMsg[i] = '\0'; cout<<"Original Message: "<<msg; cout<<"\nKey: "<<key; cout<<"\nNew Generated Key: "<<newKey; cout<<"\nEncrypted Message: "<<encryptedMsg; cout<<"\nDecrypted Message: "<<decryptedMsg; return 0; }
Comment below if you have queries or found anything incorrect in above tutorial for vigenere cipher in C and C++.
The post Vigenere Cipher in C and C++ appeared first on The Crazy Programmer.
from The Crazy Programmer http://www.thecrazyprogrammer.com/2017/08/vigenere-cipher-c-c.html
Comments
Post a Comment