Avatar of chynh

chynh's solution

to Atbash Cipher in the TypeScript Track

Published at Oct 07 2019 · 0 comments
Test suite

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.


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


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



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.


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.


import AtbashCipher from './atbash-cipher'

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

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

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

    xit("encode no", () => {
      const cipherText = atbash.encode("no")

    xit("encode OMG", () => {
      const cipherText = atbash.encode("OMG")

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

    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")

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

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

    xit("decode all the letters", () => {
      const cipherText = atbash.decode("gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt")
class AtbashCipher {
  private convert(char: string): string {
    if (!isNaN(parseInt(char))) {
      return char;
    const baseCharCode = 97;
    const baseDifference = 25;
    const distanceFromBaseCharCode = char.charCodeAt(0) - baseCharCode;
    const subtractVal = baseDifference - (2 * distanceFromBaseCharCode);
    return String.fromCharCode(char.charCodeAt(0) + subtractVal);

  public encode(word: string): string {
    const chunks: string[] = [];
    let tempStr: string = ""
    const wordArr: string[] = word.toLowerCase().replace(/[^\w]/g, "").split("");

    wordArr.forEach((char, i) => {
      tempStr += this.convert(char);
      if (tempStr.length === 5 || wordArr[i + 1] === undefined) {
        tempStr = "";
    return chunks.join(" ");
  public decode(word: string) {
    return word.replace(/\s+/g, "").replace(/./g, this.convert);

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?