73 std::string encrypt (
const std::string &text,
const std::string &key) {
74 std::string encrypted_text =
"";
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]);
79 int place_value_key = get_value(key[j]);
80 place_value_text = (place_value_text + place_value_key) % 26;
81 char encrypted_char = get_char(place_value_text);
82 encrypted_text += encrypted_char;
84 return encrypted_text;
92 std::string decrypt (
const std::string &text,
const std::string &key) {
95 std::string 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]);
98 int place_value_key = get_value(key[j]);
99 place_value_text = (place_value_text - place_value_key + 26) % 26;
100 char decrypted_char = get_char(place_value_text);
101 decrypted_text += decrypted_char;
103 return decrypted_text;
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;
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;