vigenere_cipher.cpp File Reference

Implementation of Vigenère cipher algorithm. More...

`#include <iostream>`
`#include <string>`
`#include <cassert>`
Include dependency graph for vigenere_cipher.cpp:

## Namespaces

| ciphers
| Algorithms for encryption and decryption.
| vigenere
| Functions for vigenère cipher algorithm.

## Functions

std::string | ciphers::vigenere::encrypt (const std::string &text, const std::string &key)
std::string | ciphers::vigenere::decrypt (const std::string &text, const std::string &key)
void | test ()
int | main ()

## Detailed Description

Implementation of Vigenère cipher algorithm.

The Vigenère cipher is a method of encrypting alphabetic text by using a series of interwoven vigenere ciphers, based on the letters of a keyword. It employs a form of polyalphabetic substitution.

### Algorithm

The encryption can also be represented using modular arithmetic by first transforming the letters into numbers, according to the scheme, A → 0, B → 1, ..., Z → 25. Encryption of \(i^{th}\) character in Message M by key K can be described mathematically as,

\[ E_{K}(M_{i}) = (M_{i} + K_{i})\;\mbox{mod}\; 26\]

while decryption of \(i^{th}\) character in Cipher C by key K can be described mathematically as,

\[ D_{k}(C_{i}) = (C_{i} - K_{i} + 26)\;\mbox{mod}\; 26\]

Where \(K_{i}\) denotes corresponding character in key. If \(|key| < |text|\) than same key is repeated untill their lengths are equal.

For Example, If M = "ATTACKATDAWN" and K = "LEMON" than K becomes "LEMONLEMONLE".

Note
  Rather than creating new key of equal length this program does this by using modular index for key (i.e. \((j + 1) \;\mbox{mod}\; |\mbox{key}|\))
  This program implements Vigenère cipher for only uppercase English alphabet characters (i.e. A-Z).

Author
  Deep Raval

## Function Documentation

## decrypt()

std::string ciphers::vigenere::decrypt | ( | const std::string & | text,
|  | const std::string & | key
| ) |  |

Decrypt given text using vigenere cipher.

Parameters

  text | text to be decrypted
  key | key to be used for decryption

Returns
  new decrypted text

{
// Going through each character of text and key
// Note that key is visited in circular way hence j = (j + 1) % |key|
std::string decrypted_text = ""; // Empty string to store decrypted text
for(size_t i = 0, j = 0; i < text.length(); i++, j = (j + 1) % key.length()) {
int place_value_text = get_value(text[i]); // Getting value of character in text
int place_value_key = get_value(key[j]); // Getting value of character in key
place_value_text = (place_value_text - place_value_key + 26) % 26; // Applying decryption
char decrypted_char = get_char(place_value_text); // Getting new character from decrypted value
decrypted_text += decrypted_char; // Appending decrypted character
}
return decrypted_text; // Returning decrypted text
}
std::string
std::string::length
T length(T... args)
Here is the call graph for this function:

## encrypt()

std::string ciphers::vigenere::encrypt | ( | const std::string & | text,
|  | const std::string & | key
| ) |  |

Encrypt given text using vigenere cipher.

Parameters

  text | text to be encrypted
  key | to be used for encryption

Returns
  new encrypted text

{
std::string encrypted_text = ""; // Empty string to store encrypted text
// Going through each character of text and key
// Note that key is visited in circular way hence j = (j + 1) % |key|
for(size_t i = 0, j = 0; i < text.length(); i++, j = (j + 1) % key.length()) {
int place_value_text = get_value(text[i]); // Getting value of character in text
int place_value_key = get_value(key[j]); // Getting value of character in key
place_value_text = (place_value_text + place_value_key) % 26; // Applying encryption
char encrypted_char = get_char(place_value_text); // Getting new character from encrypted value
encrypted_text += encrypted_char; // Appending encrypted character
}
return encrypted_text; // Returning encrypted text
}
Here is the call graph for this function:

## main()

int main | ( | void |  | ) |

Driver Code

{
// Testing
test();
return 0;
}
test
void test()
Definition: vigenere_cipher.cpp:111
Here is the call graph for this function:

## test()

void test | ( |  | ) |

Function to test above algorithm

{
// Test 1
std::string text1 = "NIKOLATESLA";
std::string encrypted1 = ciphers::vigenere::encrypt(text1, "TESLA");
std::string decrypted1 = ciphers::vigenere::decrypt(encrypted1, "TESLA");
assert(text1 == decrypted1);
std::cout << "Original text : " << text1;
std::cout << " , Encrypted text (with key = TESLA) : " << encrypted1;
std::cout << " , Decrypted text : "<< decrypted1 << std::endl;
// Test 2
std::string text2 = "GOOGLEIT";
std::string encrypted2 = ciphers::vigenere::encrypt(text2, "REALLY");
std::string decrypted2 = ciphers::vigenere::decrypt(encrypted2, "REALLY");
assert(text2 == decrypted2);
std::cout << "Original text : " << text2;
std::cout << " , Encrypted text (with key = REALLY) : " << encrypted2;
std::cout << " , Decrypted text : "<< decrypted2 << std::endl;
}
std::cout
std::endl
T endl(T... args)
ciphers::vigenere::decrypt
std::string decrypt(const std::string &text, const std::string &key)
Definition: vigenere_cipher.cpp:92
ciphers::vigenere::encrypt
std::string encrypt(const std::string &text, const std::string &key)
Definition: vigenere_cipher.cpp:73
Here is the call graph for this function: