Avatar of ChowSinWon

ChowSinWon's solution

to Atbash Cipher in the TypeScript Track

Published at Aug 06 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

Setup

Go through the setup instructions for TypeScript to install the necessary dependencies:

https://exercism.io/tracks/typescript/installation

Requirements

Install assignment dependencies:

$ yarn install

Making the test suite pass

Execute the tests with:

$ yarn test

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by changing xit to it.

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.ts

import AtbashCipher from './atbash-cipher'

describe('AtbashCipher', () => {
  let atbash: AtbashCipher

  beforeAll(() => {
    atbash = new AtbashCipher()
  })

  describe("encoding", () => {
    it("encode yes", () => {
      const cipherText = atbash.encode("yes")
      expect(cipherText).toEqual("bvh")
    })

    xit("encode no", () => {
      const cipherText = atbash.encode("no")
      expect(cipherText).toEqual("ml")
    })

    xit("encode OMG", () => {
      const cipherText = atbash.encode("OMG")
      expect(cipherText).toEqual("lnt")
    })

    xit("encode spaces", () => {
      const cipherText = atbash.encode("O M G")
      expect(cipherText).toEqual("lnt")
    })

    xit("encode mindblowingly", () => {
      const cipherText = atbash.encode("mindblowingly")
      expect(cipherText).toEqual("nrmwy oldrm tob")
    })

    xit("encode numbers", () => {
      const cipherText = atbash.encode("Testing,1 2 3, testing.")
      expect(cipherText).toEqual("gvhgr mt123 gvhgr mt")
    })

    xit("encode deep thought", () => {
      const cipherText = atbash.encode("Truth is fiction.")
      expect(cipherText).toEqual("gifgs rhurx grlm")
    })

    xit("encode all the letters", () => {
      const cipherText = atbash.encode("thequickbrownfoxjumpsoverthelazydog")
      expect(cipherText).toEqual("gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt")
    })
  })

  xdescribe("decode", () => {
    xit("decode exercism", () => {
      const plainText = atbash.decode("vcvix rhn")
      expect(plainText).toEqual("exercism")
    })

    xit("decode a sentence", () => {
      const cipherText = atbash.decode("zmlyh gzxov rhlug vmzhg vkkrm thglm v")
      expect(cipherText).toEqual("anobstacleisoftenasteppingstone")
    })

    xit("decode numbers", () => {
      const plainText = atbash.decode("gvhgr mt123 gvhgr mt")
      expect(plainText).toEqual("testing123testing")
    })

    xit("decode all the letters", () => {
      const cipherText = atbash.decode("gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt")
      expect(cipherText).toEqual("thequickbrownfoxjumpsoverthelazydog")
    })
  })
})
class AtbashCipher {
  public encode(text:string):string {
    return this.grouped(this.translate(text));
  }

  public decode(code:string):string {
    return this.translate(code);
  }

  private translate(text:string):string {
    const chars:Array<string> = text.toLowerCase().split('');
    let result:string = '';
    for (let x=0; x<chars.length; x++) {
      const charCode:number = chars[x].charCodeAt(0);
      if (charCode >= 97 && charCode <= 122) {
        result += String.fromCharCode(219 - charCode);
      }
      else if (charCode >= 48 && charCode <= 57) {
        result += chars[x];
      }
    }

    return result;
  }

  private grouped(code:string) {
    let result = '';
    for (let i=0; i<Math.ceil(code.length/5); i++) {
      result += code.substr(i*5, 5) + ' ';
    }

    return result.trim();
  }
}

export default AtbashCipher;

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?