# Algorithms_in_C++: ciphers/atbash_cipher.cpp File Reference

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:

## 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 More...
static void | test ()
| Self-test implementations. More...
int | main ()
| Main function. More...

## 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

## Function Documentation

## atbash_cipher()

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

atbash cipher encryption and decryption

Parameters

  text | Plaintext to be encrypted

Returns
  encoded or decoded string

{
std::string result;
for (char letter : text) {
result += atbash_cipher_map[letter];
}
return result;
}
std::string
math::fibonacci_sum::result
uint64_t result(uint64_t n)
Definition: fibonacci_sum.cpp:76
Here is the call graph for this function:

## main()

int main | ( | void |  | ) |

Main function.

Returns
  0 on exit

{
test(); // run self-test implementations
return 0;
}
test
static void test()
Self-test implementations.
Definition: atbash_cipher.cpp:62
Here is the call graph for this function:

## test()

static void test | ( |  | ) |

static

Self-test implementations.

Returns
  void

{
// 1st test
std::string text = "Hello World";
std::string expected = "Svool Dliow";
std::string encrypted_text = ciphers::atbash::atbash_cipher(text);
std::string decrypted_text = ciphers::atbash::atbash_cipher(encrypted_text);
assert(expected == encrypted_text);
assert(text == decrypted_text);
std::cout << "Original text: " << text << std::endl;
std::cout << ", Expected text: " << expected << std::endl;
std::cout << ", Encrypted text: " << encrypted_text << std::endl;
std::cout << ", Decrypted text: " << decrypted_text << std::endl;
std::cout << "\nAll tests have successfully passed!\n";
}
ciphers::atbash::atbash_cipher
std::string atbash_cipher(const std::string &text)
atbash cipher encryption and decryption
Definition: atbash_cipher.cpp:47
std::cout
std::endl
T endl(T... args)
Here is the call graph for this function:

## 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'}, {' ', ' '}
}