Avatar of w1zeman1p

w1zeman1p's solution

to Atbash Cipher in the Ruby Track

Published at Feb 25 2019 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution 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
class Atbash
  PLAIN = 'abcdefghijklmnopqrstuvwxyz'
  CIPHER = 'zyxwvutsrqponmlkjihgfedcba'

  def self.encode(input)
    input
      .delete(" .,")
      .downcase
      .tr(PLAIN, CIPHER)
      .scan(/.{5}|.*$/)
      .join(" ")
      .strip
  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?