TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
atbash_cipher.cpp
Go to the documentation of this file.
1
15#include <cassert>
16#include <iostream>
17#include <map>
18#include <string>
19
23namespace ciphers {
28namespace atbash {
29std::map<char, char> atbash_cipher_map = {
30 {'a', 'z'}, {'b', 'y'}, {'c', 'x'}, {'d', 'w'}, {'e', 'v'}, {'f', 'u'},
31 {'g', 't'}, {'h', 's'}, {'i', 'r'}, {'j', 'q'}, {'k', 'p'}, {'l', 'o'},
32 {'m', 'n'}, {'n', 'm'}, {'o', 'l'}, {'p', 'k'}, {'q', 'j'}, {'r', 'i'},
33 {'s', 'h'}, {'t', 'g'}, {'u', 'f'}, {'v', 'e'}, {'w', 'd'}, {'x', 'c'},
34 {'y', 'b'}, {'z', 'a'}, {'A', 'Z'}, {'B', 'Y'}, {'C', 'X'}, {'D', 'W'},
35 {'E', 'V'}, {'F', 'U'}, {'G', 'T'}, {'H', 'S'}, {'I', 'R'}, {'J', 'Q'},
36 {'K', 'P'}, {'L', 'O'}, {'M', 'N'}, {'N', 'M'}, {'O', 'L'}, {'P', 'K'},
37 {'Q', 'J'}, {'R', 'I'}, {'S', 'H'}, {'T', 'G'}, {'U', 'F'}, {'V', 'E'},
38 {'W', 'D'}, {'X', 'C'}, {'Y', 'B'}, {'Z', 'A'}, {' ', ' '}
39
40};
41
47std::string atbash_cipher(const std::string& text) {
48 std::string result;
49 for (char letter : text) {
50 result += atbash_cipher_map[letter];
51 }
52 return result;
53}
54
55} // namespace atbash
56} // namespace ciphers
57
62static void test() {
63 // 1st test
64 std::string text = "Hello World";
65 std::string expected = "Svool Dliow";
66 std::string encrypted_text = ciphers::atbash::atbash_cipher(text);
67 std::string decrypted_text = ciphers::atbash::atbash_cipher(encrypted_text);
68 assert(expected == encrypted_text);
69 assert(text == decrypted_text);
70 std::cout << "Original text: " << text << std::endl;
71 std::cout << ", Expected text: " << expected << std::endl;
72 std::cout << ", Encrypted text: " << encrypted_text << std::endl;
73 std::cout << ", Decrypted text: " << decrypted_text << std::endl;
74 std::cout << "\nAll tests have successfully passed!\n";
75}
76
81int main() {
82 test(); // run self-test implementations
83 return 0;
84}
std::string atbash_cipher(const std::string &text)
atbash cipher encryption and decryption
static void test()
Self-test implementations.
int main()
Main function.
Functions for the Atbash Cipher implementation.
Algorithms for encryption and decryption.