Skip to content

Instantly share code, notes, and snippets.

@arn-e
Created September 20, 2012 00:30
Show Gist options
  • Save arn-e/3753228 to your computer and use it in GitHub Desktop.
Save arn-e/3753228 to your computer and use it in GitHub Desktop.
BabyCrypto
class BabyCrypto
def decrypt(key, value)
((key.class == Fixnum) || (key.to_i != 0)) ? offset = key.to_s.split.map {|i| i.to_i} : offset = calc_offset(key)
decrypted, off_i = [], 1
value.split('').each_with_index do |val, i|
val.ord >= 97 && val.ord <= 122 ? dist = val.ord + (offset[off_i -1] % 26) : dist = val.ord
dist > 122 && dist!= val.ord ? (decrypted.push (122 - (26 - (dist - 122))).chr) : (decrypted.push dist.chr)
off_i % (offset.length) == 0 ? off_i = 1 : off_i = off_i + 1
end
return decrypted.join
end
def encrypt(key, value)
((key.class == Fixnum) || (key.to_i != 0)) ? offset = key.to_s.split.map {|i| i.to_i} : offset = calc_offset(key)
encrypted, off_i = [], 1
value.split('').each_with_index do |val, i|
val.ord >= 97 && val.ord <= 122 ? dist = val.ord - (offset[off_i -1] % 26) : dist = val.ord
dist < 97 && dist != val.ord ? (encrypted.push (122 - (97 - (dist+1))).chr) : (encrypted.push dist.chr)
off_i % (ocffset.length) == 0 ? off_i = 1 : off_i = off_i + 1
end
return encrypted.join
end
def calc_offset(key)
offset, key_1, key_2 = [], key.split[0].split(''), key.split[1].split('')
key_1.each_with_index do |val,i|
dist = key_2[i].ord - val.ord
dist >= 0 ? offset[i] = dist : offset[i] = dist + 26
end
return offset
end
def freq_anal(msg)
freq,msg_arr,sum = Hash.new(0),msg.split(''),0
msg_arr.each_with_index {|i,j| msg_arr.delete_at(j) if i == " "}.each {|i| freq[i],sum = freq[i] + 1,sum +1}
return freq.each {|a,b| freq[a] = calc_perc(b,sum)}.sort_by {|i,j| j}.reverse
end
def freq_anal_i(msg)
freq,f_freq,msg_arr,sum = Hash.new(0),Hash.new(0),msg.split(' '),0
msg_arr.each {|i| i.split('').each_with_index do |a,b|
f_freq[a] += 1 if b == 0
freq[a],sum = freq[a] + 1, sum +1
end}
end
def calc_perc(val,total)
return ((val.to_f / total.to_f) * 100).round(3)
end
end
#sub = BabyCrypto.new
#puts sub.freq_anal("hi hello how are you? What is this I am kind of just writing random stuff in order to get my super awesome word frequency calc enough of a sample input... will this work yet? maybe maybe yes no maybe so.")
#add on : frequency analysis
#break a polyalphabetic cipher by determining the length of the shift word, comparing intervals
#one time pad
# sub.encrypt("baz maa", "I'm strictly plug-and-play, I ain't afraid of Y2K")
# sub.decrypt("baz maa", "I'l ssgibilx pkjg-pnc-pkpy, I pim't pfqpic oe Y2K")
# sub.encrypt("aczayt mmtopq", "I'm down with Bill Gates, I call him \"Money\" for short")
# sub.decrypt("aczayt mmtopq", "I's mrkd irwv Buuo Ggfnv, I ldzb trp \"Mawhm\" rxu inaaw")
# sub.encrypt(1,"I phone him up at home and I make him do my tech support")
# sub.decrypt(1,"I ognmd ghl to zs gnld zmc I lzjd ghl cn lx sdbg rtoonqs")
# sub.encrypt("892","It's all about the Pentiums, what?")
# sub.decrypt("892","Il'k sdd stgml lzw Pwflamek, ozsl?")
# sub.encrypt("1","hello cipher")
# sub.decrypt("1","gdkkn bhogdq")
# sub.encrypt("ykzwi pqrst", "it's all about the pentiums, what?")
# sub.decrypt("ykzwi pqrst", "rn'w jft pkicx cbm enhbmjvm, lqub?")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment