Avatar of paulfioravanti

paulfioravanti's solution

to Simple Cipher in the Ruby Track

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

Implement a simple shift cipher like Caesar and a more secure substitution cipher.

Step 1

"If he had anything confidential to say, he wrote it in cipher, that is, by so changing the order of the letters of the alphabet, that not a word could be made out. If anyone wishes to decipher these, and get at their meaning, he must substitute the fourth letter of the alphabet, namely D, for A, and so with the others." —Suetonius, Life of Julius Caesar

Ciphers are very straight-forward algorithms that allow us to render text less readable while still allowing easy deciphering. They are vulnerable to many forms of cryptanalysis, but we are lucky that generally our little sisters are not cryptanalysts.

The Caesar Cipher was used for some messages from Julius Caesar that were sent afield. Now Caesar knew that the cipher wasn't very good, but he had one ally in that respect: almost nobody could read well. So even being a couple letters off was sufficient so that people couldn't recognize the few words that they did know.

Your task is to create a simple shift cipher like the Caesar Cipher. This image is a great example of the Caesar Cipher:

Caesar Cipher

For example:

Giving "iamapandabear" as input to the encode function returns the cipher "ldpdsdqgdehdu". Obscure enough to keep our message secret in transit.

When "ldpdsdqgdehdu" is put into the decode function it would return the original "iamapandabear" letting your friend read your original message.

Step 2

Shift ciphers are no fun though when your kid sister figures it out. Try amending the code to allow us to specify a key and use that for the shift distance. This is called a substitution cipher.

Here's an example:

Given the key "aaaaaaaaaaaaaaaaaa", encoding the string "iamapandabear" would return the original "iamapandabear".

Given the key "ddddddddddddddddd", encoding our string "iamapandabear" would return the obscured "ldpdsdqgdehdu"

In the example above, we've set a = 0 for the key value. So when the plaintext is added to the key, we end up with the same message coming out. So "aaaa" is not an ideal key. But if we set the key to "dddd", we would get the same thing as the Caesar Cipher.

Step 3

The weakest link in any cipher is the human being. Let's make your substitution cipher a little more fault tolerant by providing a source of randomness and ensuring that the key contains only lowercase letters.

If someone doesn't submit a key at all, generate a truly random key of at least 100 characters in length.

If the key submitted is not composed only of lowercase letters, your solution should handle the error in a language-appropriate way.

Extensions

Shift ciphers work by making the text slightly odd, but are vulnerable to frequency analysis. Substitution ciphers help that, but are still very vulnerable when the key is short or if spaces are preserved. Later on you'll see one solution to this problem in the exercise "crypto-square".

If you want to go farther in this field, the questions begin to be about how we can exchange keys in a secure way. Take a look at Diffie-Hellman on Wikipedia for one of the first implementations of this scheme.


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

To include color from the command line:

ruby -r minitest/pride simple_cipher_test.rb

Source

Substitution Cipher at Wikipedia http://en.wikipedia.org/wiki/Substitution_cipher

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

simple_cipher_test.rb

require 'minitest/autorun'
require_relative 'simple_cipher'

class RandomKeyCipherTest < Minitest::Test
  def setup
    @cipher = Cipher.new
  end

  def test_cipher_key_is_letters
    assert_match(/\A[a-z]+\z/, @cipher.key)
  end

  # Here we take advantage of the fact that plaintext of "aaa..." doesn't
  # outputs the key. This is a critical problem with shift ciphers, some
  # characters will always output the key verbatim.
  def test_cipher_encode
    skip
    plaintext = 'aaaaaaaaaa'
    assert_equal(@cipher.key[0, 10], @cipher.encode(plaintext))
  end

  def test_cipher_decode
    skip
    plaintext = 'aaaaaaaaaa'
    assert_equal(plaintext, @cipher.decode(@cipher.key[0, 10]))
  end

  def test_cipher_reversible
    skip
    plaintext = 'abcdefghij'
    assert_equal(plaintext, @cipher.decode(@cipher.encode(plaintext)))
  end
end

class IncorrectKeyCipherTest < Minitest::Test
  def test_cipher_with_caps_key
    skip
    assert_raises ArgumentError do
      Cipher.new('ABCDEF')
    end
  end

  def test_cipher_with_numeric_key
    skip
    assert_raises ArgumentError do
      Cipher.new('12345')
    end
  end

  def test_cipher_with_empty_key
    skip
    assert_raises ArgumentError do
      Cipher.new('')
    end
  end
end

class SubstitutionCipherTest < Minitest::Test
  def setup
    @key = 'abcdefghij'
    @cipher = Cipher.new(@key)
  end

  def test_cipher_key_is_as_submitted
    skip
    assert_equal(@cipher.key, @key)
  end

  def test_cipher_encode
    skip
    plaintext = 'aaaaaaaaaa'
    ciphertext = 'abcdefghij'
    assert_equal(ciphertext, @cipher.encode(plaintext))
  end

  def test_cipher_decode
    skip
    plaintext = 'aaaaaaaaaa'
    ciphertext = 'abcdefghij'
    assert_equal(plaintext, @cipher.decode(ciphertext))
  end

  def test_cipher_reversible
    skip
    plaintext = 'abcdefghij'
    assert_equal(plaintext, @cipher.decode(@cipher.encode(plaintext)))
  end

  def test_double_shift_encode
    skip
    plaintext = 'iamapandabear'
    ciphertext = 'qayaeaagaciai'
    assert_equal(ciphertext, Cipher.new('iamapandabear').encode(plaintext))
  end

  def test_cipher_encode_wrap
    skip
    plaintext = 'zzzzzzzzzz'
    ciphertext = 'zabcdefghi'
    assert_equal(ciphertext, @cipher.encode(plaintext))
  end
end

class PseudoShiftCipherTest < Minitest::Test
  def setup
    @cipher = Cipher.new('dddddddddd')
  end

  def test_cipher_encode
    skip
    plaintext = 'aaaaaaaaaa'
    ciphertext = 'dddddddddd'
    assert_equal(ciphertext, @cipher.encode(plaintext))
  end

  def test_cipher_decode
    skip
    plaintext = 'aaaaaaaaaa'
    ciphertext = 'dddddddddd'
    assert_equal(plaintext, @cipher.decode(ciphertext))
  end

  def test_cipher_reversible
    skip
    plaintext = 'abcdefghij'
    assert_equal(plaintext, @cipher.decode(@cipher.encode(plaintext)))
  end
end
# frozen_string_literal: true

class Cipher
  A_ORDINAL = "a".ord
  private_constant :A_ORDINAL
  ALPHABET = ("a".."z").to_a.freeze
  private_constant :ALPHABET
  RANDOM_KEY = -> { (0...100).map { ALPHABET.sample }.join }
  private_constant :RANDOM_KEY
  VALID_KEY = /\A[a-z]+\z/
  private_constant :VALID_KEY

  attr_reader :key

  def initialize(key = RANDOM_KEY.call)
    raise ArgumentError unless key.match?(VALID_KEY)
    @key = key
  end

  def encode(plaintext)
    shift(plaintext, :+)
  end

  def decode(ciphertext)
    shift(ciphertext, :-)
  end

  private

  def shift(text, message)
    text
      .chars
      .lazy
      .zip(key.chars.cycle)
      .with_object(message)
      .map(&method(:substitute))
      .force
      .join
  end

  def substitute((text_char, key_char), message)
    [text_char, key_char]
      .map { |char| char.ord - A_ORDINAL }
      .reduce(message)
      .modulo(ALPHABET.count)
      .+(A_ORDINAL)
      .chr
  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?