Crypto With Kids: Ruby Rotator

Created on March 10, 2015 by Richard Kelly
Tags: ruby, cryptography

Bletchley Park Naval Enigma IMG 3604

Crypto Wheel

This was just a fun script I wrote to help solve alphabet rotation puzzles, like the Caesar cipher, or ROT-13.

It shows all possible rotations of a given script. Since humans are so good at seeing patterns like words, it’s pretty easy to pick out the solution from among the choices.

#!/bin/ruby

# Tested under Ruby 2.2
# Usage: ./rotate.rb 'OCDN DN EPNO KMVXODXZ'
# ..
# $ THIS IS JUST PRACTICE (key: 5)
# ..

# Only accept upper case
if ARGV[0] != ARGV[0].upcase
    abort('Error: illegal input')
end

coded = ARGV[0]
puts coded + ' (encrypted text)'

first = 'A'.ord
last = 'Z'.ord
range = last - first + 1

for i in 1..25
    for pos in 0..coded.length - 1
        if coded[pos] != ' '
        	print (((coded[pos].ord - first + i) % range) + first).chr # to activate up/down, add i>> + (pos % 2 * 17)<<) % range
        else
                print ' '
        end
    end
    print ' (key: ' + i.to_s + ')'
    print "\n"
end