Avatar of paulfioravanti

paulfioravanti's solution

to Atbash Cipher in the Ruby Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code 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

For installation and learning resources, refer to the Ruby resources page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

gem install minitest

If you would like color output, you can require 'minitest/pride' in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

ruby atbash_cipher_test.rb

To include color from the command line:

ruby -r minitest/pride atbash_cipher_test.rb

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

require 'minitest/autorun'
require_relative 'atbash_cipher'

class AtbashTest < Minitest::Test
  def test_encode_no
    assert_equal 'ml', Atbash.encode('no')
  end

  def test_encode_yes
    skip
    assert_equal 'bvh', Atbash.encode('yes')
  end

  def test_encode_OMG
    skip
    assert_equal 'lnt', Atbash.encode('OMG')
  end

  def test_encode_O_M_G
    skip
    assert_equal 'lnt', Atbash.encode('O M G')
  end

  def test_encode_long_word
    skip
    assert_equal 'nrmwy oldrm tob', Atbash.encode('mindblowingly')
  end

  def test_encode_numbers
    skip
    assert_equal('gvhgr mt123 gvhgr mt',
                 Atbash.encode('Testing, 1 2 3, testing.'))
  end

  def test_encode_sentence
    skip
    assert_equal 'gifgs rhurx grlm', Atbash.encode('Truth is fiction.')
  end

  def test_encode_all_the_things
    skip
    plaintext = 'The quick brown fox jumps over the lazy dog.'
    cipher = 'gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt'
    assert_equal cipher, Atbash.encode(plaintext)
  end
end
# frozen_string_literal: true

module Atbash
  ALPHABET = ("a".."z").to_a.join
  private_constant :ALPHABET
  NON_LETTERS = /[[[:blank:]][[:punct:]]]/.freeze
  private_constant :NON_LETTERS
  TEXT_GROUPING = /(.{5})(?=.)/.freeze
  private_constant :TEXT_GROUPING
  GROUPING_OUTPUT = "\\1 \\2"
  private_constant :GROUPING_OUTPUT

  module_function

  def encode(plaintext)
    plaintext
      .downcase
      .gsub(NON_LETTERS, "")
      .tr(ALPHABET, ALPHABET.reverse)
      .gsub(TEXT_GROUPING, GROUPING_OUTPUT)
  end
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?