TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
atbash_cipher.cpp File Reference

Atbash Cipher implementation More...

#include <cassert>
#include <iostream>
#include <map>
#include <string>
Include dependency graph for atbash_cipher.cpp:

Go to the source code of this file.

Namespaces

namespace  ciphers
 Algorithms for encryption and decryption.
 
namespace  atbash
 Functions for the Atbash Cipher implementation.
 

Functions

std::string ciphers::atbash::atbash_cipher (const std::string &text)
 atbash cipher encryption and decryption
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

Variables

std::map< char, char > ciphers::atbash::atbash_cipher_map
 

Detailed Description

Atbash Cipher implementation

The Atbash cipher is a subsitution cipher where the letters of the alphabet are in reverse. For example, A is replaced with Z, B is replaced with Y, etc.

Algorithm

The algorithm takes a string, and looks up the corresponding reversed letter for each letter in the word and replaces it. Spaces are ignored and case is preserved.

Author
Focusucof

Definition in file atbash_cipher.cpp.

Function Documentation

◆ atbash_cipher()

std::string ciphers::atbash::atbash_cipher ( const std::string & text)

atbash cipher encryption and decryption

Parameters
textPlaintext to be encrypted
Returns
encoded or decoded string

Definition at line 47 of file atbash_cipher.cpp.

47 {
48 std::string result;
49 for (char letter : text) {
50 result += atbash_cipher_map[letter];
51 }
52 return result;
53}
uint64_t result(uint64_t n)

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 81 of file atbash_cipher.cpp.

81 {
82 test(); // run self-test implementations
83 return 0;
84}
static void test()
Self-test implementations.

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void

Definition at line 62 of file atbash_cipher.cpp.

62 {
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}
std::string atbash_cipher(const std::string &text)
atbash cipher encryption and decryption

Variable Documentation

◆ atbash_cipher_map

std::map<char, char> ciphers::atbash::atbash_cipher_map
Initial value:
= {
{'a', 'z'}, {'b', 'y'}, {'c', 'x'}, {'d', 'w'}, {'e', 'v'}, {'f', 'u'},
{'g', 't'}, {'h', 's'}, {'i', 'r'}, {'j', 'q'}, {'k', 'p'}, {'l', 'o'},
{'m', 'n'}, {'n', 'm'}, {'o', 'l'}, {'p', 'k'}, {'q', 'j'}, {'r', 'i'},
{'s', 'h'}, {'t', 'g'}, {'u', 'f'}, {'v', 'e'}, {'w', 'd'}, {'x', 'c'},
{'y', 'b'}, {'z', 'a'}, {'A', 'Z'}, {'B', 'Y'}, {'C', 'X'}, {'D', 'W'},
{'E', 'V'}, {'F', 'U'}, {'G', 'T'}, {'H', 'S'}, {'I', 'R'}, {'J', 'Q'},
{'K', 'P'}, {'L', 'O'}, {'M', 'N'}, {'N', 'M'}, {'O', 'L'}, {'P', 'K'},
{'Q', 'J'}, {'R', 'I'}, {'S', 'H'}, {'T', 'G'}, {'U', 'F'}, {'V', 'E'},
{'W', 'D'}, {'X', 'C'}, {'Y', 'B'}, {'Z', 'A'}, {' ', ' '}
}

Definition at line 29 of file atbash_cipher.cpp.

29 {
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};