Skip to content

Instantly share code, notes, and snippets.

@marktabler
Created October 13, 2011 07:31
Show Gist options
  • Save marktabler/1283656 to your computer and use it in GitHub Desktop.
Save marktabler/1283656 to your computer and use it in GitHub Desktop.
Penny-Arcade Cipher Decryptor
class WordIndexCipher
attr_accessor :source_text, :character_table
def initialize(text)
@source_text = text.upcase.split(' ')
make_character_table
end
def encode(text)
@cipher = ""
text.upcase.split('').reject{|char| char == " "}.each do |character|
if @character_table[character].empty?
abort "Cannot encode - the character '#{character}' does not appear in your source text."
else
@cipher << @character_table[character].shuffle.first << " "
end
end
@cipher
end
def decode(text)
@plaintext = ""
text.split(' ').each do |chunk|
word_index, character_index = chunk.split('-')
@plaintext << @source_text[word_index.to_i-1][character_index.to_i-1] << " "
end
@plaintext
end
private
def make_character_table
@character_table = Hash.new{|hash, key| hash[key] = []}
@source_text.each_with_index do |word, word_index|
word.split('').each_with_index do |character, character_index|
@character_table[character] << "#{word_index + 1}-#{character_index + 1}"
end
end
end
end
pa_cipher = WordIndexCipher.new('sir i need you to turn off your my what you want me to turn off my book oh youd like that i bet youd love to turn off all books wouldntcha yeah just like hitler uh oh too far youre gonna shock my genitals huh the genital region i think we might start there yeah')
puts pa_cipher.decode('28-1 10-2 10-3 6-4 18-4 43-1 36-3 9-2 43-4 32-9 41-2 1-1 2-1 45-1 42-3 33-2 3-4 42-4 14-2 26-1 32-10 35-3 54-3 56-1 3-3')
@j3j3
Copy link

j3j3 commented May 16, 2012

What an amazing gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment