By Sri Sakthi M

Implementation of Encryption and Decryption of plaintext using Vigenere Cipher in C++. Vigenere Cipher is a kind of polyalphabetic substitution method of encrypting alphabetic text.

The Encryption is done using a (26*26) matrix or a table.

Vigenere Cipher Table is used in which alphabets from A to Z are written in 26 rows, for encryption and decryption in this method.

**Key:** LOCK

**Message:** GIVE MONEY

Repeat the letters in Key till the length of the key becomes equal to the length of the message. Here the length of the message and key becomes equal.

To perform encryption take the first letter of the message and key in this case G and L. Find the character which coincides with row G and column L. The coinciding letter is R. Perform this for all other characters.

Repeat the same process for all remaining alphabets.

The encrypted text is :

**Encrypted Message:** RWXOXCPOJ.

The ciphertext can be generated by the below equation.

Ei = (P + K) mod 26

Here P is plain text and K is key.

**Key:** LOCK

**Encrypted Message:** RWXOXCPOJ

Here we have to Repeat the letters in Key till the length of the key becomes equal to the length of the encrypted message. Here the length of the encrypted message and key becomes equal.

To perform decryption take the first alphabet of the encrypted message, in this case, R, and the first alphabet of the key which is L. Find the coinciding element for L and R, the coinciding alphabet is G. Follow the same for all alphabets.

**Original Message: **GIVE MONEY

This can be represented in algebraic form by the following equations.

Pi = (E – K + 26) mod 26

Below is the C++ program

#include <bits/stdc++.h> using namespace std; class V{ public: string s; V(string s) { for (int i = 0; i < s.size(); ++i) { if (s[i] >= 'A' && s[i] <= 'Z') this->s += s[i]; else if (s[i] >= 'a' && s[i] <= 'z') this->s += s[i] + 'A' - 'a'; } } string encryption(string t) { string out; for (int i = 0, j = 0; i < t.length(); ++i) { char c = t[i]; if (c >= 'a' && c <= 'z') c += 'A' - 'a'; else if (c < 'A' || c > 'Z') continue; out += (c + s[j] - 2 * 'A') % 26 + 'A'; j = (j + 1) % s.length(); } return out; } string decryption(string t) { string out; for (int i = 0, j = 0; i < t.length(); ++i) { char c = t[i]; if (c >= 'a' && c <= 'z') c += 'A' - 'a'; else if (c < 'A' || c > 'Z') continue; out += (c - s[j] + 26) % 26 + 'A'; j = (j + 1) % s.length(); } return out; } }; int main() { V v("LOCK"); string ori ="GIVEMONEY"; string encrypt = v.encryption(ori); string decrypt = v.decryption(encrypt); cout << "Original Message: "<<ori<< endl; cout << "Encrypted Message: " << encrypt << endl; cout << "Decrypted Message: " << decrypt << endl; }

Original Message: GIVEMONEY Encrypted Message: RWXOXCPOJ Decrypted Message: GIVEMONEY

Submitted by Sri Sakthi M (Sakthi)

Download packets of source code on Coders Packet

## Comments