Skip to content

Instantly share code, notes, and snippets.

@Scoutski
Created October 14, 2015 03:49
Show Gist options
  • Save Scoutski/84e25ee8c50a8d4817ec to your computer and use it in GitHub Desktop.
Save Scoutski/84e25ee8c50a8d4817ec to your computer and use it in GitHub Desktop.
Crypto Square Solution
class Crypto
def initialize phrase
@phrase = phrase
end
def normalize_plaintext
@phrase.gsub(/[^0-9a-zA-Z]/, "").downcase
end
def size
Math.sqrt(normalize_plaintext.length).ceil
end
def plaintext_segments
normalize_plaintext.scan(/.{1,#{size}}/)
end
def ciphertext
cipher_text = []
plaintext_segments.each do |segment|
segment.chars.each_with_index do |letter, i|
cipher_text[i] ||= []
cipher_text[i] << letter
end
end
cipher_text.join
end
def normalize_ciphertext
ciphertext.scan(/.{1,5}/).join(" ")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment