TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
vigenere_cipher.cpp
Go to the documentation of this file.
1
33#include <iostream>
34#include <string>
35#include <cassert>
36
40namespace ciphers {
44 namespace vigenere {
45 namespace {
51 inline char get_char(const int x) {
52 // By adding 65 we are scaling 0-25 to 65-90.
53 // Which are in fact ASCII values of A-Z.
54 return char(x + 65);
55 }
61 inline int get_value(const char c) {
62 // A-Z have ASCII values in range 65-90.
63 // Hence subtracting 65 will scale them to 0-25.
64 return int(c - 65);
65 }
66 } // Unnamed namespace
73 std::string encrypt (const std::string &text, const std::string &key) {
74 std::string encrypted_text = ""; // Empty string to store encrypted text
75 // Going through each character of text and key
76 // Note that key is visited in circular way hence j = (j + 1) % |key|
77 for(size_t i = 0, j = 0; i < text.length(); i++, j = (j + 1) % key.length()) {
78 int place_value_text = get_value(text[i]); // Getting value of character in text
79 int place_value_key = get_value(key[j]); // Getting value of character in key
80 place_value_text = (place_value_text + place_value_key) % 26; // Applying encryption
81 char encrypted_char = get_char(place_value_text); // Getting new character from encrypted value
82 encrypted_text += encrypted_char; // Appending encrypted character
83 }
84 return encrypted_text; // Returning encrypted text
85 }
92 std::string decrypt (const std::string &text, const std::string &key) {
93 // Going through each character of text and key
94 // Note that key is visited in circular way hence j = (j + 1) % |key|
95 std::string decrypted_text = ""; // Empty string to store decrypted text
96 for(size_t i = 0, j = 0; i < text.length(); i++, j = (j + 1) % key.length()) {
97 int place_value_text = get_value(text[i]); // Getting value of character in text
98 int place_value_key = get_value(key[j]); // Getting value of character in key
99 place_value_text = (place_value_text - place_value_key + 26) % 26; // Applying decryption
100 char decrypted_char = get_char(place_value_text); // Getting new character from decrypted value
101 decrypted_text += decrypted_char; // Appending decrypted character
102 }
103 return decrypted_text; // Returning decrypted text
104 }
105 } // namespace vigenere
106} // namespace ciphers
107
111void test() {
112 // Test 1
113 std::string text1 = "NIKOLATESLA";
114 std::string encrypted1 = ciphers::vigenere::encrypt(text1, "TESLA");
115 std::string decrypted1 = ciphers::vigenere::decrypt(encrypted1, "TESLA");
116 assert(text1 == decrypted1);
117 std::cout << "Original text : " << text1;
118 std::cout << " , Encrypted text (with key = TESLA) : " << encrypted1;
119 std::cout << " , Decrypted text : "<< decrypted1 << std::endl;
120 // Test 2
121 std::string text2 = "GOOGLEIT";
122 std::string encrypted2 = ciphers::vigenere::encrypt(text2, "REALLY");
123 std::string decrypted2 = ciphers::vigenere::decrypt(encrypted2, "REALLY");
124 assert(text2 == decrypted2);
125 std::cout << "Original text : " << text2;
126 std::cout << " , Encrypted text (with key = REALLY) : " << encrypted2;
127 std::cout << " , Decrypted text : "<< decrypted2 << std::endl;
128}
129
131int main() {
132 // Testing
133 test();
134 return 0;
135}
Algorithms for encryption and decryption.
Functions for vigenère cipher algorithm.
char get_char(const std::string &input, std::size_t pos)
Returns the character at pos after the input is padded.
Definition sha256.cpp:170
void test()
int main()