Skip to content

Instantly share code, notes, and snippets.

@d0ppler
Created April 14, 2015 09:26
Show Gist options
  • Save d0ppler/2d8d1f37cfaebc29ac7a to your computer and use it in GitHub Desktop.
Save d0ppler/2d8d1f37cfaebc29ac7a to your computer and use it in GitHub Desktop.
cipher
####
# Counts the characters of the encrypted message and returns
# the number of characters that needs to be generated and added
# to the message so that every encrypted message has a total
# of 100 characters (target).
def generate_fill(string)
target = 100
chars_left = target - string.size
return ([*('0'..'9'),*('a'..'z')]).sample(chars_left.to_i).join
end
cipher = {
"a" => ['1q24t', 'akO17'],
"b" => ['5T15q', 'x1m45'],
"c" => ['1p97x', '9zP23'],
"d" => ['9z22z', '7qM61'],
"e" => ['1r18i', '5ik80'],
"f" => ['3P42e', '9tv12'],
"g" => ['7j80e', '13y25'],
"h" => ['1k51w', 'u6c74'],
"i" => ['6c85c', 'gT399'],
"j" => ['9V36v', 'z1P58'],
"k" => ['3L88j', '0hi92'],
"l" => ['6j11o', 'e7r33'],
"m" => ['1b82y', 'j1k26'],
"n" => ['2y43e', 'kXO91'],
"o" => ['7h48q', 'W1e12'],
"p" => ['1Z10p', 'M9y53'],
"q" => ['9B32o', '5sf48'],
"r" => ['7W77l', 'n3n27'],
"s" => ['3s43k', '20l85'],
"t" => ['7cY5b', '88o93'],
"u" => ['8i14n', '0Ri04'],
"v" => ['9R81s', '4a118'],
"w" => ['1q43a', 'tU081'],
"x" => ["1a02s", "pA323"],
"y" => ["9o00e", "i8j35"],
"z" => ["1j69y", "D7x91"]
}
prompt = ">> "
puts "Enter a message you would like to encrypt.", prompt
input = gets.chomp
encrypted_content = input.gsub(Regexp.union(cipher.keys)) {|m| cipher[m].sample }
fill = generate_fill(encrypted_content)
output = encrypted_content + fill
puts "\n***************************************************************************************************"
puts "Encrypted message: #{output}"
puts "***************************************************************************************************"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment