Avatar of MinaYossry

MinaYossry's solution

to Atbash Cipher in the C++ Track

Published at Jul 29 2019 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

Create an implementation of the atbash cipher, an ancient encryption system created in the Middle East.

The Atbash cipher is a simple substitution cipher that relies on transposing all the letters in the alphabet such that the resulting alphabet is backwards. The first letter is replaced with the last letter, the second with the second-last, and so on.

An Atbash cipher for the Latin alphabet would be as follows:

Plain:  abcdefghijklmnopqrstuvwxyz
Cipher: zyxwvutsrqponmlkjihgfedcba

It is a very weak cipher because it only has one possible key, and it is a simple monoalphabetic substitution cipher. However, this may not have been an issue in the cipher's time.

Ciphertext is written out in groups of fixed length, the traditional group size being 5 letters, and punctuation is excluded. This is to make it harder to guess things based on word boundaries.

Examples

  • Encoding test gives gvhg
  • Decoding gvhg gives test
  • Decoding gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt gives thequickbrownfoxjumpsoverthelazydog

Getting Started

Make sure you have read the Installing and Running the Tests pages for C++ on exercism.io. This covers the basic information on setting up the development environment expected by the exercises.

Passing the Tests

Get the first test compiling, linking and passing by following the three rules of test-driven development. Create just enough structure by declaring namespaces, functions, classes, etc., to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, uncomment the next test by moving the following line past the next test.

#if defined(EXERCISM_RUN_ALL_TESTS)

This may result in compile errors as new constructs may be invoked that you haven't yet declared or defined. Again, fix the compile errors minimally to get a failing test, then change the code minimally to pass the test, refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C++11 facilities in preference to writing your own low-level algorithms or facilities by hand. CppReference is a wiki reference to the C++ language and standard library. If you are new to C++, but have programmed in C, beware of C traps and pitfalls.

Source

Wikipedia http://en.wikipedia.org/wiki/Atbash

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

atbash_cipher_test.cpp

#include "atbash_cipher.h"
#include "test/catch.hpp"

TEST_CASE("encode_yes")
{
    REQUIRE("bvh" == atbash::encode("yes"));
}

#if defined(EXERCISM_RUN_ALL_TESTS)
TEST_CASE("encode_no")
{
    REQUIRE("ml" == atbash::encode("no"));
}

TEST_CASE("encode_OMG")
{
    REQUIRE("lnt" == atbash::encode("OMG"));
}

TEST_CASE("encode_spaces")
{
    REQUIRE("lnt" == atbash::encode("O M G"));
}

TEST_CASE("encode_mindblowingly")
{
    REQUIRE("nrmwy oldrm tob" == atbash::encode("mindblowingly"));
}

TEST_CASE("encode_numbers")
{
    REQUIRE("gvhgr mt123 gvhgr mt" == atbash::encode("Testing,1 2 3, testing."));
}

TEST_CASE("encode_deep_thought")
{
    REQUIRE("gifgs rhurx grlm" == atbash::encode("Truth is fiction."));
}

TEST_CASE("encode_all_the_letters")
{
    REQUIRE("gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt" ==
                        atbash::encode("The quick brown fox jumps over the lazy dog."));
}

TEST_CASE("decode_exercism")
{
    REQUIRE("exercism" == atbash::decode("vcvix rhn"));
}

TEST_CASE("decode_a_sentence")
{
    REQUIRE("anobstacleisoftenasteppingstone" ==
                        atbash::decode("zmlyh gzxov rhlug vmzhg vkkrm thglm v"));
}

TEST_CASE("decode_numbers")
{
    REQUIRE("testing123testing" == atbash::decode("gvhgr mt123 gvhgr mt"));
}

TEST_CASE("decode_all_the_letters")
{
    REQUIRE("thequickbrownfoxjumpsoverthelazydog" ==
                        atbash::decode("gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt"));
}
#endif

atbash_cipher.cpp

#include "atbash_cipher.h"

namespace atbash {
	std::string encode(const std::string& text) {
		int count{};
		std::string cipher;
		for (size_t i{}; i < text.size(); ) {
			char currentChar = tolower(text.at(i));
			if (!isalnum(currentChar)) {
				i++;
				continue;
			}
			else if (count < 5) {
				if (isalpha(currentChar)) {
					cipher += reversedAlpha.at(currentChar - 'a');
				}
				else if (isdigit(currentChar)) {
					cipher += currentChar;
				}	
				count++;
				i++;
			}
			else {
				cipher += ' ';
				count = 0;
			}
		}
		return cipher;
	}
	std::string decode(const std::string& cipher) {
		std::string text;
		for (size_t i{}; i < cipher.size(); i++) {
			char currentChar = tolower(cipher.at(i));
			if (isspace(currentChar))
				continue;
			else if (isdigit(currentChar))
				text += currentChar;
			else
				text += reversedAlpha.at(currentChar - 'a');
		}
		return text;
	}
}

atbash_cipher.h

#ifndef ATBASH_CIPHER_H
#define ATBASH_CIPHER_H

#include <string>

namespace atbash {
	const static std::string reversedAlpha{ "zyxwvutsrqponmlkjihgfedcba" };
	std::string encode(const std::string& text);
	std::string decode(const std::string& cipher);
}

#endif

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?