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

Loading...
Searching...
No Matches
a1z26_cipher.cpp File Reference

Implementation of the A1Z26 cipher More...

`#include <algorithm>`
`#include <cassert>`
`#include <cstdint>`
`#include <iostream>`
`#include <map>`
`#include <sstream>`
`#include <string>`
`#include <vector>`
Include dependency graph for a1z26_cipher.cpp:

## Namespaces

namespace | ciphers
| Algorithms for encryption and decryption.
namespace | a1z26
| Functions for A1Z26 encryption and decryption implementation.

## Functions

std::string | ciphers::a1z26::encrypt (std::string text)
| a1z26 encryption implementation More...
std::string | ciphers::a1z26::decrypt (const std::string &text, bool bReturnUppercase=false)
| a1z26 decryption implementation More...
static void | test ()
| Self-test implementations. More...
int | main ()
| Main function. More...

## Variables

std::map< uint8_t, char > | ciphers::a1z26::a1z26_decrypt_map
std::map< char, uint8_t > | ciphers::a1z26::a1z26_encrypt_map

## Detailed Description

Implementation of the A1Z26 cipher

The A1Z26 cipher is a simple substiution cipher where each letter is replaced by the number of the order they're in. For example, A corresponds to 1, B = 2, C = 3, etc.

Author
  Focusucof

## Function Documentation

## decrypt()

std::string ciphers::a1z26::decrypt | ( | const std::string & | text,
|  | bool | bReturnUppercase = `false`
| ) |  |

a1z26 decryption implementation

Parameters

  text | is the encrypted text input
  bReturnUppercase | is if the decoded string should be in uppercase or not

Returns
  the decrypted string in all uppercase or all lowercase

{
std::string result;
// split words seperated by spaces into a vector array
std::vector<std::string> word_array;
std::stringstream sstream(text);
std::string word;
while (sstream >> word) {
word_array.push_back(word);
}
for (auto& i : word_array) {
std::replace(i.begin(), i.end(), '-', ' ');
std::vector<std::string> text_array;
std::stringstream ss(i);
std::string res_text;
while (ss >> res_text) {
text_array.push_back(res_text);
}
for (auto& i : text_array) {
result += a1z26_decrypt_map[stoi(i)];
}
result += ' ';
}
result.pop_back(); // remove any leading whitespace
if (bReturnUppercase) {
std::transform(result.begin(), result.end(), result.begin(), ::toupper);
}
return result;
}
std::string
std::stringstream
math::fibonacci_sum::result
uint64_t result(uint64_t n)
Definition: fibonacci_sum.cpp:76
std::vector::push_back
T push_back(T... args)
std::replace
T replace(T... args)
std::stoi
T stoi(T... args)
std::transform
T transform(T... args)
std::vector
Here is the call graph for this function:

## encrypt()

std::string ciphers::a1z26::encrypt | ( | std::string | text | ) |

a1z26 encryption implementation

Parameters

  text | is the plaintext input

Returns
  encoded string with dashes to seperate letters

{
std::string result;
std::transform(text.begin(), text.end(), text.begin(),
::tolower); // convert string to lowercase
std::replace(text.begin(), text.end(), ':', ' ');
for (char letter : text) {
if (letter != ' ') {
result += std::to_string(
a1z26_encrypt_map[letter]); // convert int to string and append
// to result
result += "-"; // space out each set of numbers with spaces
} else {
result.pop_back();
result += ' ';
}
}
result.pop_back(); // remove leading dash
return result;
}
std::string::begin
T begin(T... args)
std::string::end
T end(T... args)
std::to_string
T to_string(T... args)
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: a1z26_cipher.cpp:120
Here is the call graph for this function:

## test()

static void test | ( |  | ) |

static

Self-test implementations.

Returns
  void

{
// 1st test
std::string input = "Hello World";
std::string expected = "8-5-12-12-15 23-15-18-12-4";
std::string output = ciphers::a1z26::encrypt(input);
std::cout << "Input: " << input << std::endl;
std::cout << "Expected: " << expected << std::endl;
std::cout << "Output: " << output << std::endl;
assert(output == expected);
std::cout << "TEST PASSED";
// 2nd test
input = "12-15-23-5-18-3-1-19-5";
expected = "lowercase";
output = ciphers::a1z26::decrypt(input);
std::cout << "Input: " << input << std::endl;
std::cout << "Expected: " << expected << std::endl;
std::cout << "Output: " << output << std::endl;
assert(output == expected);
std::cout << "TEST PASSED";
// 3rd test
input = "21-16-16-5-18-3-1-19-5";
expected = "UPPERCASE";
output = ciphers::a1z26::decrypt(input, true);
std::cout << "Input: " << input << std::endl;
std::cout << "Expected: " << expected << std::endl;
std::cout << "Output: " << output << std::endl;
assert(output == expected);
std::cout << "TEST PASSED";
}
ciphers::a1z26::decrypt
std::string decrypt(const std::string &text, bool bReturnUppercase=false)
a1z26 decryption implementation
Definition: a1z26_cipher.cpp:78
ciphers::a1z26::encrypt
std::string encrypt(std::string text)
a1z26 encryption implementation
Definition: a1z26_cipher.cpp:51
std::cout
std::endl
T endl(T... args)
Here is the call graph for this function:

## Variable Documentation

## a1z26_decrypt_map

std::map<uint8_t, char> ciphers::a1z26::a1z26_decrypt_map

Initial value:
= {
{1, 'a'}, {2, 'b'}, {3, 'c'}, {4, 'd'}, {5, 'e'}, {6, 'f'}, {7, 'g'},
{8, 'h'}, {9, 'i'}, {10, 'j'}, {11, 'k'}, {12, 'l'}, {13, 'm'}, {14, 'n'},
{15, 'o'}, {16, 'p'}, {17, 'q'}, {18, 'r'}, {19, 's'}, {20, 't'}, {21, 'u'},
{22, 'v'}, {23, 'w'}, {24, 'x'}, {25, 'y'}, {26, 'z'},
}

## a1z26_encrypt_map

std::map<char, uint8_t> ciphers::a1z26::a1z26_encrypt_map

Initial value:
= {
{'a', 1}, {'b', 2}, {'c', 3}, {'d', 4}, {'e', 5}, {'f', 6}, {'g', 7},
{'h', 8}, {'i', 9}, {'j', 10}, {'k', 11}, {'l', 12}, {'m', 13}, {'n', 14},
{'o', 15}, {'p', 16}, {'q', 17}, {'r', 18}, {'s', 19}, {'t', 20}, {'u', 21},
{'v', 22}, {'w', 23}, {'x', 24}, {'y', 25}, {'z', 26}}