TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Implementation of Vigenère cipher algorithm. More...
#include <iostream>
#include <string>
#include <cassert>
Go to the source code of this file.
Namespaces | |
namespace | ciphers |
Algorithms for encryption and decryption. | |
namespace | 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 () |
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.
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".
Definition in file vigenere_cipher.cpp.
std::string ciphers::vigenere::decrypt | ( | const std::string & | text, |
const std::string & | key ) |
Decrypt given text using vigenere cipher.
text | text to be decrypted |
key | key to be used for decryption |
Definition at line 92 of file vigenere_cipher.cpp.
std::string ciphers::vigenere::encrypt | ( | const std::string & | text, |
const std::string & | key ) |
Encrypt given text using vigenere cipher.
text | text to be encrypted |
key | to be used for encryption |
Definition at line 73 of file vigenere_cipher.cpp.
int main | ( | void | ) |
Driver Code
Definition at line 131 of file vigenere_cipher.cpp.
void test | ( | ) |
Function to test above algorithm
Definition at line 111 of file vigenere_cipher.cpp.