🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of AndrewUyeda

AndrewUyeda's solution

to Atbash Cipher in the Objective-C Track

Published at Jul 13 2018 · 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

Setup

There are two different methods of getting set up to run the tests with Objective-C:

  • Create an Xcode project with a test target which will run the tests.
  • Use the ruby gem objc as a test runner utility.

Both are described in more detail here: http://exercism.io/languages/objective-c

Submitting Exercises

When submitting an exercise, make sure your solution file is in the same directory as the test code.

The submit command will look something like:

exercism submit <path-to-exercism-workspace>/objective-c/atbash-cipher/AtbashCipher.m

You can find the Exercism workspace by running exercism debug and looking for the line beginning with Workspace.

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.

AtbashCipherTest.m

#import <XCTest/XCTest.h>

#if __has_include("AtbashCipherExample.h")
# import "AtbashCipherExample.h"
# else
# import "AtbashCipher.h"
#endif

@interface AtbashCipherTest : XCTestCase

@end

@implementation AtbashCipherTest

- (void)testEncodeNo {
    XCTAssertEqualObjects(@"ml", [AtbashCipher encode:@"no"]);
}

- (void)testEncodeYes {
    XCTAssertEqualObjects(@"bvh", [AtbashCipher encode:@"yes"]);
}

- (void)testEncodeOMG {
    XCTAssertEqualObjects(@"lnt", [AtbashCipher encode:@"OMG"]);
}

- (void)testEncodeOMGWithSpaces {
    XCTAssertEqualObjects(@"lnt", [AtbashCipher encode:@"O M G"]);
}

- (void)testEncodeLongWord {
    XCTAssertEqualObjects(@"nrmwy oldrm tob", [AtbashCipher encode:@"mindblowingly"]);
}

- (void)testEncodeNumbers {
    XCTAssertEqualObjects(@"gvhgr mt123 gvhgr mt", [AtbashCipher encode:@"Testing, 1 2 3, testing."]);
}

- (void)testEncodeSentence {
    XCTAssertEqualObjects(@"gifgs rhurx grlm", [AtbashCipher encode:@"Truth is fiction."]);
}

- (void)testEncodeAllTheThings {
    NSString *plaintext = @"The quick brown fox jumps over the lazy dog.";
    NSString *cipher = @"gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt";
    XCTAssertEqualObjects(cipher, [AtbashCipher encode:plaintext]);
}

@end
#import <XCTest/XCTest.h>

#if __has_include("atbashcipherExample.h")
# import "atbashcipherExample.h"
# else
# import "atbashcipher.h"
#endif

@interface atbashcipherTest : XCTestCase

@end

@implementation atbashcipherTest

- (void)testEncodeNo {
    XCTAssertEqualObjects(@"ml", [atbashcipher encode:@"no"]);
}

- (void)testEncodeYes {
    XCTAssertEqualObjects(@"bvh", [atbashcipher encode:@"yes"]);
}

- (void)testEncodeOMG {
    XCTAssertEqualObjects(@"lnt", [atbashcipher encode:@"OMG"]);
}

- (void)testEncodeOMGWithSpaces {
    XCTAssertEqualObjects(@"lnt", [atbashcipher encode:@"O M G"]);
}

- (void)testEncodeLongWord {
    XCTAssertEqualObjects(@"nrmwy oldrm tob", [atbashcipher encode:@"mindblowingly"]);
}

- (void)testEncodeNumbers {
    XCTAssertEqualObjects(@"gvhgr mt123 gvhgr mt", [atbashcipher encode:@"Testing, 1 2 3, testing."]);
}

- (void)testEncodeSentence {
    XCTAssertEqualObjects(@"gifgs rhurx grlm", [atbashcipher encode:@"Truth is fiction."]);
}

- (void)testEncodeAllTheThings {
    NSString *plaintext = @"The quick brown fox jumps over the lazy dog.";
    NSString *cipher = @"gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt";
    XCTAssertEqualObjects(cipher, [atbashcipher encode:plaintext]);
}

@end

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?