Skip to content

Instantly share code, notes, and snippets.

@MaggieMoss
Created May 10, 2015 19:17
Show Gist options
  • Save MaggieMoss/3fc81febdc7ef55b0848 to your computer and use it in GitHub Desktop.
Save MaggieMoss/3fc81febdc7ef55b0848 to your computer and use it in GitHub Desktop.
Playing with the basic ceasar cipher
def create_ceasar_cipher(phrase, key)
letters = phrase.scan(/./)
new_phrase = []
letters.each do |letter|
num = letter.ord
num += key
new_letter = num.chr
new_phrase << new_letter
end
puts new_phrase.join("")
end
def solve_ceasar_cipher(phrase, key)
letters = phrase.scan(/./)
new_phrase = []
letters.each do |letter|
num = letter.ord
num -= key
new_letter = num.chr
new_phrase << new_letter
end
return new_phrase.join("");
end
def break_ceasar_cipher
i = 0
num = -50
all_solutions = []
while num < 0 do
solution = solve_ceasar_cipher("'5p\"?<<p?%\"p?'>p3\"$) ?", num)
all_solutions << solution
num +=1
end
while i < all_solutions.length do
possible_solution = all_solutions[i].downcase
if contains_english(possible_solution)
puts possible_solution
end
i += 1
end
end
def contains_english(phrase)
common_letter_combinations = ["the", "ee", "tt", "oo", "ss", "ll", "ff", "the", "he", "re", "an", "th", "er", "in", "ion", "and", "ing", "ent"]
i = 0
while i < common_letter_combinations.length-1 do
i += 1
if phrase.include? common_letter_combinations[i]
return true
end
end
end
break_ceasar_cipher
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment