Skip to content

Instantly share code, notes, and snippets.

@xtoddx
Created June 3, 2010 00:55
Show Gist options
  • Save xtoddx/423259 to your computer and use it in GitHub Desktop.
Save xtoddx/423259 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
print "MESSAGE> "
message = gets.gsub(/\s/, '').split(//)
print "PAD> "
pad = gets.gsub(/\s/, '').split(//)
message.zip(pad) do |mc, pc|
puts "#{mc} | #{pc} | #{'%2i' % (nm = mc[0] - ?a)} | #{'%2i' % (np = pc[0] - ?a)} | #{'%2i' % ((nm + np) % 26)} | #{(((nm + np) % 26) + ?a).chr}"
end
cyphertext = message.zip(pad).collect do |m,p|
(?a + (((m[0] - ?a) + (p[0] - ?a)) % 26)).chr
end
puts "CYPHERTEXT: #{cyphertext}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment