TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
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
23
namespace
ciphers
{
28
namespace
atbash
{
29
std::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
47
std::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
62
static
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
81
int
main
() {
82
test
();
// run self-test implementations
83
return
0;
84
}
ciphers::atbash::atbash_cipher
std::string atbash_cipher(const std::string &text)
atbash cipher encryption and decryption
Definition
atbash_cipher.cpp:47
test
void test()
Definition
caesar_cipher.cpp:100
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
atbash
Functions for the Atbash Cipher implementation.
ciphers
Algorithms for encryption and decryption.
ciphers
atbash_cipher.cpp
Generated by
1.18.0