Avatar of shikov

shikov's solution

to Atbash Cipher in the C Track

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

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 "Guides" section of the C track on the Exercism site. 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.

The included makefile can be used to create and run the tests using the test task.

make test

Create just the functions you need 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, move onto the next test.

As you progress through the tests, take the time to refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C99 facilities in preference to writing your own low-level algorithms or facilities by hand.

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.

test_atbash_cipher.c

#include "vendor/unity.h"
#include "../src/atbash_cipher.h"
#include <stdlib.h>
#include <string.h>

void setUp(void)
{
}

void tearDown(void)
{
}

static void test_encode(char *input, char *expected)
{
   char *result = atbash_encode(input);

   TEST_ASSERT_EQUAL_STRING(expected, result);

   free(result);
}

static void test_decode(char *input, char *expected)
{
   char *result = atbash_decode(input);

   TEST_ASSERT_EQUAL_STRING(expected, result);

   free(result);
}

static void test_encode_yes(void)
{
   test_encode("yes", "bvh");
}

static void test_encode_no(void)
{
   TEST_IGNORE();
   test_encode("no", "ml");
}

static void test_encode_OMG(void)
{
   TEST_IGNORE();
   test_encode("OMG", "lnt");
}

static void test_encode_spaces(void)
{
   TEST_IGNORE();
   test_encode("O M G", "lnt");
}

static void test_encode_mindblowingly(void)
{
   TEST_IGNORE();
   test_encode("mindblowingly", "nrmwy oldrm tob");
}

static void test_encode_numbers(void)
{
   TEST_IGNORE();
   test_encode("Testing,1 2 3, testing.", "gvhgr mt123 gvhgr mt");
}

static void test_encode_deep_thought(void)
{
   TEST_IGNORE();
   test_encode("Truth is fiction.", "gifgs rhurx grlm");
}

static void test_encode_all_the_letters(void)
{
   TEST_IGNORE();
   test_encode("The quick brown fox jumps over the lazy dog.",
               "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt");
}

static void test_encode_ignores_non_ascii(void)
{
   TEST_IGNORE();
   test_encode("non ascii éignored", "mlmzh xrrrt mlivw");
}

static void test_decode_exercism(void)
{
   TEST_IGNORE();
   test_decode("vcvix rhn", "exercism");
}

static void test_decode_a_sentence(void)
{
   TEST_IGNORE();
   test_decode("zmlyh gzxov rhlug vmzhg vkkrm thglm v",
               "anobstacleisoftenasteppingstone");
}

static void test_decode_numbers(void)
{
   TEST_IGNORE();
   test_decode("gvhgr mt123 gvhgr mt", "testing123testing");
}

static void test_decode_all_the_letters(void)
{
   TEST_IGNORE();
   test_decode("gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt",
               "thequickbrownfoxjumpsoverthelazydog");
}

int main(void)
{
   UnityBegin("test/test_atbash_cipher.c");

   RUN_TEST(test_encode_yes);
   RUN_TEST(test_encode_no);
   RUN_TEST(test_encode_OMG);
   RUN_TEST(test_encode_spaces);
   RUN_TEST(test_encode_mindblowingly);
   RUN_TEST(test_encode_numbers);
   RUN_TEST(test_encode_deep_thought);
   RUN_TEST(test_encode_all_the_letters);
   RUN_TEST(test_encode_ignores_non_ascii);

   RUN_TEST(test_decode_exercism);
   RUN_TEST(test_decode_a_sentence);
   RUN_TEST(test_decode_numbers);
   RUN_TEST(test_decode_all_the_letters);

   return UnityEnd();
}

src/atbash_cipher.h

#ifndef ATBASH_CIPHER_H
#define ATBASH_CIPHER_H

#define MAX_STR_LEN 100

char* atbash_encode(char* input);
char* atbash_decode(char* input);

#endif

src/atbash_cipher.c

#include "atbash_cipher.h"
#include <stdlib.h>
#include <ctype.h>

char* atbash_encode(char* input){
    char* str = calloc(MAX_STR_LEN, sizeof (char));
    size_t str_i=0, char_len = 0;
    for (size_t i=0; input[i]!='\0'; i++) {
        if(input[i]<0 || !isalnum(input[i])) continue;
        if(str_i>0 && !(char_len%5)) str[str_i++] = ' ';
        if(!isalpha(input[i])) str[str_i++] = input[i];
        else str[str_i++] = (char)(219-tolower(input[i]));
        char_len++;
    }
    return str;
}

char* atbash_decode(char* input){
    char* str = calloc(MAX_STR_LEN, sizeof (char));
    size_t str_i=0;
    for (size_t i=0; input[i]!='\0'; i++) {
        if(input[i] == ' ') continue;
        if(!isalpha(input[i])) str[str_i++] = input[i];
        else str[str_i++] = 219-input[i];
    }
    return str;
}

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?