Skip to content

Instantly share code, notes, and snippets.

@worace
Created April 6, 2016 01:53
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 worace/6aeec90a7023c1832915c793263a0880 to your computer and use it in GitHub Desktop.
Save worace/6aeec90a7023c1832915c793263a0880 to your computer and use it in GitHub Desktop.
require 'date'
require 'pry'
class KeyGenerator
def self.create_key
rand(10000..99999).to_s
end
end
class Offset
attr_reader :key, :date, :final_key, :final_offset, :code
def initialize(key = nil, date = nil)
@key = key || KeyGenerator.create_key
@date = date || Date.today
raise Error if @key.size != 5 || @key.class != String
format_key(@key)
format_offset(@date)
end
def format_key(key)
split_key = key.to_s.chars
@final_key = []
# permutations?
(split_key.size - 1).times do |num|
@final_key << split_key[num] + split_key[num+1]
end
@final_key.map!(&:to_i)
end
def format_offset(offset)
offset = offset.strftime("%d%m%y") if offset.class == Date
@final_offset = (offset.to_i ** 2).to_s[-4..-1].chars.map { |num| num.to_i }
end
def add_them_together
@code = [@final_key, @final_offset]
@code = @code.transpose.map { |value| value.reduce(:+)}
end
end
class Enigma
attr_reader :message, :key, :date, :message_index, :shift, :encrypted_message, :temp_array
def initialize
@character_map = [*("a".."z"), *("0".."9"), " ", ".", ","]
end
def encrypt(message, key=nil, date=nil, mode = "forward")
magic_crypt_part(message, key, date, mode)
@encrypted_message
end
def decrypt(message, key=nil, date=nil, mode = "backward")
magic_crypt_part(message, key, date, mode)
@encrypted_message
end
def crack(message=nil, date=nil)
@message = message
@date = date || Date.today
offset = Offset.new(nil, @date)
0.upto(99999) do |key|
temp_key = key.to_s.rjust(5, "0")
temp_message = decrypt(@message, temp_key, @date)
return temp_array = [temp_message, temp_key] if temp_message[-7..-1] == "..end.."
end
end
def magic_crypt_part(message, key=nil, date=nil, mode)
@message = message.downcase
@key = key || KeyGenerator.create_key
@date = date || Date.today
@shift = Offset.new(@key, @date)
@shift.add_them_together
message_to_index(@message)
rotate_and_return(mode)
end
def message_to_index(message)
@message_index = message.chars.map do |letter|
@character_map.index(letter)
end
end
def rotate_and_return(mode)
e_message = []
@message_index.each_with_index do |message, index|
if mode == "forward"
rotated = @character_map.rotate(@shift.code[index % 4])
elsif mode =="backward"
rotated = @character_map.rotate(-@shift.code[index % 4])
end
e_message << rotated[message]
end
@encrypted_message = e_message.join
end
end
encryptions = [
# { :student=> "Charles Kaminer", :encryption_date=>"040416", :key=>"15440", :ciphertext=> ".woew2otdmszpvo0.xxnprvv th06mmysrup5vh0zthl5r1578sv5mksy31p.wwfpryt49xpup3p62hzwr4y088e07hj99mps0iexxxkpu4y.woypxxz.64j.xyu mk0pvs0z9lev33eu3we 0kzzmm196sj,04tp7vh whi33le 0kzzmwh 8oyp7vh whz691jwm2ss7re76yqwr3e 0kzzmouyxwhp7vh whlc8ou xyupskzzmmo,2uzp9xkw62j66oe6uh15soy ryywm2pcmnv.mwkpnil5sif"},
# { :student=> "Denali Lord", :encryption_date=>"050226", :key=>"39704", :ciphertext=> "k1drh 74s.7phxiho1jswxahee73kx7mktokf,b1vqqrhe7md674vx7sqqqrhqjov. qhq xgqtkq,7kavekqzbiayfxgqfxv,o4f,fyq.7kwqdsw0rlawl3avlwa.ikv07mx osfai4pqpvd.ehe4lla.ikv07wd.qouqpvd.ehv7r1fx72otpra8oymx,3a.ikv07oqzfwdqpvd.ehhdqoq.fyqqakv074s8b1ftpoaaknh pmr bhf0 1dvqou.7nr,7wgq8ih6aib"},
# { :student=> "Erin Greenhalgh", :encryption_date=>"010310", :key=>"14750", :ciphertext=> "3mu9rog9oitvotq9 fdvtiw9vfc22qg9okc4ovq ozg3vudv8fvyvfhz,m,uzok osgcpfyvovgvufe5um,a5zf9pfhz4l,z40v8.kvz5vuor1,xz1j.sff5 fe53fu2r0jot2t8zkw2.u,92iuyojn5sfu2r0jo3iu vz,92iuyo0q.8kgo9tc9yfr85rgt fu2r0jovviz3i,92iuyomz vvuz5v,ur0jo2mv vz,.4lg89kq8vfmvcff5 fouogav4lap"},
# { :student=> "Jason Hanna", :encryption_date=>"040411", :key=>"21950", :ciphertext=> "e00o2603jq49vz0 e19xvv75dxt ,qy8yv6z.zt 5xtv.vaca,45.qw247aze08pvv 3 a9z0tcz,6t92vd86,ho6.ttcayzy4uo319uvyd8e008v199e dte1 4dqw vz4 5axo17co078od4w95qy.c 4tf4d3v.7rd0ts97xod4w95q8rd,08v.7rd0t9,aat2qb2y.3oa 02vcod4w95q04418rv.7rd0tvi,04d1 4vww95qyyf669va9u2 bt, 0o,yt..w08dv 82qbziqz5eq8uvruv.wup"},
# { :student=> "Ji Kim", :encryption_date=>"120412", :key=>"19923", :ciphertext=> "33z 0,qk.aq80z40735.czw0xgqj3zq53v 3ybxhbs, 0gq5w8qkbzq.9s, 0s57bat90stdzsc39bq3tx0391x1t01dzs1dbb kyb1e9aq3csz.c2a4ty7jtx7cta43b2q5d, .yc4k8s.bwa00x674ta43b2qcwa,7as.bwa00b9ahyzqi7v. t e5zvjta43b2q7911cws.bwa000f,79a1e9sw3b2qk. xhyv.7tc660,.5 ,x0y2thwx,7aaq6 bqczsr108w1u"},
# { :student=> "Joshua Cohen", :encryption_date=>"110412", :key=>"23456", :ciphertext=> "7dr44miecni24,wu.dx5g,ou1tid7,iz782x2opbf5444tiz0iief,i5a54445x1fnl345l 357xaoixx sxabpvxat 35t fo2e2ot.anixg5r5gc5yx.zdx z9xnwxfcizhm252pwe,5380nsu1gzyxnwxfci90n41e5380nsufj5b2,ic.834xk2.9,ndxnwxfci1abt905380nsu4s41ant.a5oxfcieckpb2831xpy04m3zbmpu2clb0 41eni0boi935jv4iovy"},
# { :student=> "Karina Gonzales", :encryption_date=>"041115", :key=>"99373", :ciphertext=> "5xji26ava7ag2to.9xpjetg.zaau5tad5pub08hsdmwi2aady2avdtaj.mwi2mpfd7dh2mdo1mzb.8abvrkb.vh,vulo1mlod8uv08lp.7abemjjewxcvsruvrrnv7obdwadf6uj09ov mvmy7k.z0rcv7obdwany7wfcmvmy7k.d3xs0tat9pviv4up7tfuv7obdwaf.vlnymvmy7k.2,wf.7lp.mgbdwava4hs0pvfv9qe26vd,6h.0wdsyrwfc7ae,8an1mb,22g,w"},
# { :student=> "Kris Sparks", :encryption_date=>"040116", :key=>"55420", :ciphertext=> "0xo7x6fh87f5xttx4xu8,tlxuafg0tf20pz0v8me.m17xaf2t2fh.tf86m17xmu4.7i6xmiawm4068f0qrp06vmyquqawmqa.8zhv8qb67f0,mo8,w21qswgqrw,q7t0.wf2a6z8v9th5m0.t7pxu0w1q7t0.wf,t714 m0.t7px.32evtff4p07q4zb2tkgq7t0.wf46vq,tm0.t7pxx,1467qb6ml0.wfh84mevp04q9v3x60276mxvwietr14 7f378f,wmgyx2lyr"},
# { :student=> "Linda McCartney", :encryption_date=>"040216", :key=>"00293", :ciphertext=> "wj,shp,71,daai,bwki1aeg9vg5br, ,def3qi5bkg5zqemgsvd9q,86jqm3wjhtaej7pwi3fco3rp5ahep,lvtslu5xuw 3dn6sikiyahp,wj,,akiawtpxwkj8v,8baidbkw9sgqosfqhsvn8ak, cutdxxnp7augvvj5woq9svn8ak,hvvv,,augvvj5arwmxh,n6ducsstj4heosvn8ak,,8jkhvaugvvj5z0v,8vkj8af8ak, 2xpfaawiyhtnxrt,srh5cqf,,vej,h,n30,.9w,hyaa6zqf6t"},
# { :student=> "Luigi Aversano", :encryption_date=>"040316", :key=>"65639", :ciphertext=> "jy7d747soo.y0x7zjzdm0tbuiv0zeo5x3taodx0z v0kdth4f .udo3r95hojyce0tesc.do5rjoe40y7tkx. od.90ih.5o321d8zdj0wkxjy7x0zdyj8kijzetio3z0x.z .4d65jd55cdi23y o50h8.ik2ks09bgiy0hb54di23y ocgi 7x09bgiy0ye.hi7oir39 df8ep7tjdi23y o7t9zcg09bgiy0kn 7tizet0u3y o5nk4ay0.dj78iie87dew00du7xitex7oiono6ujocj0p1kdu1e"},
# { :student=> "Megan McMullin", :encryption_date=>"040516", :key=>"00235", :ciphertext=> "kk8jhtzwsuzhhga,okbkwg5,e0zvkgzekcgcfv6tv,ijh0zedpzwvgzkq,ijh,bgvu2ih,2pg,lcqvzcae9cqi6aah pg, pvvgwfv qquzcw,8kwjjdafdvaedoauacvjzextgkfwawp,hndu9,enddauacvjzoduigu,hndu9,vqjtfgzuochjargqmg4vauacvjzgqi od,hndu9,hzigqu qq,5cvjzwsr6tfchgawcfhthert6,fj2tdeiguuzfrvzog,0ahp5ab"},
# { :student=> "Michael Lane Winham", :encryption_date=>"030416", :key=>"33852", :ciphertext=> "jd4. fq.7,3x7k0.q9nx,,6.b9m4ihq.7bm67m0,7qq5blnxo950b9r1sdjwffu,7jqe898x7mqxa9o7adjclqp.89r1kcj1kr5 rb51lm4q sjzfsta.9p7q9o7j944 rtq,t3 fb64rlj.i,407ax7.944 rtqj,4,bqj.i,407r0aobqqpkm.e91 liqvq944 rtqbms1j,j.i,407d9,bm41lmjw rtqid5,bqjakcq pb0 b9wxv9p7q9yw7 kxkckr"},
{ :student=> "Nick Pisciotta", :encryption_date=>"030616", :key=>"00485", :ciphertext=> "wlskhrsz1bw5aks6wm1tagz1vil6rbq4dgyvqkl6kilrqg5.sxw1qboyjs5vwl0lag2zpy1vfe7vrrl5hg84lx,klwlpuyqvdpmkim1qaj84wls4am15wv8pwm20vbo6akw6kypkgs7kfs0kvpo5kbq7uvwpxp8zawznvlloospkvpo5kb0nvxs4awznvll5ry5phb6ydwvksv2whg7kvpo5kbs0jm0nawznvllr0xs0vm20aho5kbquxry5ay1qhv6prvskrjl7qhs4vg24hb6v0br1wb0qacmrqhml"}
# { :student=> "Parker Phillips", :encryption_date=>"050416", :key=>"64925", :ciphertext=> "bo.k2qxkzk 9zv7kihu94kak6htdasxkzmtfzx7lz1xe6wu9gh,,6hyakoq8 q1lzuxq0hc9zxx95hvg5oqod1wk0hyacnqac2,jjm,adx.223q. 30m3hwgihvgbh.d220244 j madjwqkak.,zl4g3h.d2202bk.l61qkak.,z27mgmx2hvtk9h8jdtx7ih.d22026xzabkqkak.,zodl6x.adxq82202ao,l61qmcnxjhm7j6h39nhwgih58zir9cnr3"},
# { :student=> "Peter Springer", :encryption_date=>"020716", :key=>"99347", :ciphertext=> "ewel22e0jmi6vve7exnuvrl2dt 7,mc5yrkw.v 75t s.rr,a8i2.maz43rwewmmvro0 9nw0ptw,2 62ru568yl67 qc9cwy0.l3xnrvuu5ewe5vxn6e6uqexo1dma7vvi759bl13tl03mld0a65mc8c6iqf0u0v7lodw p93bld0a65mmod8e5v7lodw 6,9rq2mszy7hla6ox2rtld0a65me14xmov7lodw si8e1dxo1vsa65mcvf2k6v9nr26sq,6el,u 8.se5dro52mswimd2emmrvn.s.s.m"},
# { :student=> "Sonia Gupta", :encryption_date=>"020816", :key=>"86863", :ciphertext=> "s.t.phkl0ik9p7y1w.z,47q1mokks7k6s344njri306.pok6ldkl37k,y06.p0z83in p0neo094yjk4i5u4y9r2i8veo0ve3j4lnjvfyik440t,4 75i61ki51diiy43 k65h4,nkylx05cliu1mb15iiy43 kdli68205cliu13e7in7kjw35.if4fu7pkiiy43 k8y9vdl05cliu1pn68yivfy0q43 kl0frin358ik07ph56zhr1n nil5682ik7zjkdo0l2pdq2j"},
# { :student=> "Yoseph Anderson", :encryption_date=>"022016", :key=>"33956", :ciphertext=> "ei24brtemst2be7uii85qezu.ytdeetzeaax,t0bp c4bytz ntepet5k c4b 81psw3b w a fxkttx7c3xkg0v7f4 a 4 ptae,t4.kstxq 25qhdy7d d7c 97s7xphtzrra5,u7ej b8 s3u.l y7s7xpht9 sc1o b8 s3upodb,etciab47pa.geyd7s7xpht1kg49 b8 s3ubxc1ks4.k zxphtemp0b,ab17u90brbzlr0u,hwb cc1ost0ltt9a uvbnzv8"},
]
encryptions.each do |s|
e = Enigma.new
e.decrypt(s[:ciphertext], s[:key], s[:date])
puts "Assignment for student #{s[:student]}: #{e.encrypted_message}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment