Skip to content

Instantly share code, notes, and snippets.

@stungeye
Created May 26, 2011 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stungeye/993543 to your computer and use it in GitHub Desktop.
Save stungeye/993543 to your computer and use it in GitHub Desktop.
Obfuscated Caesar Cipher Solver
s = IO.read('code.txt')
shift = ('e'.ord - ('a'..'z').reduce(['',0]) do |m, l|
s.count(l) > m[1] ? [l, s.count(l)] : m
end[0].ord) % 26
puts s.tr('a-z', (('a'..'z').to_a*2)[shift...shift+26].join)
# Assumptions:
#
# - The plaintext is written in a language that uses Roman characters.
# - The plaintext is written in a language where 'e' is the most common letter.
# - All characters are lower case and only the alphas have been shifted.
# - RUBY_VERSION == 1.9.2
#
# Example Ciphertext:
#
# rwpeitg x. sdlc iwt gpqqxi-wdat
#
# paxrt lph qtvxccxcv id vti ktgn ixgts du hxiixcv qn wtg hxhitg dc iwt
# qpcz, pcs du wpkxcv cdiwxcv id sd: dcrt dg ilxrt hwt wps ettets xcid iwt
# qddz wtg hxhitg lph gtpsxcv, qji xi wps cd exrijgth dg rdcktghpixdch xc
# xi, 'pcs lwpi xh iwt jht du p qddz,' iwdjvwi paxrt 'lxiwdji exrijgth dg
# rdcktghpixdc?'
Smaller Version:
s=IO.read ?c
a=?a..?z
d=(101-a.reduce(['',0]){|m,l|s.count(l)>m[1]?[l,s.count(l)]:m}[0].ord)%26
puts s.tr 'a-z',(a.to_a*2)[d..d+25].join
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment