-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeo.rb
51 lines (40 loc) · 1.34 KB
/
geo.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class String
def to_num
self.downcase.bytes[0]
end
end
class Decoder
def initialize(str, offset_str)
@str = str
@offset_str = offset_str
end
def decode
i = 0
placeholder = ""
while i < @str.length
ascii_number = @str[i].to_num - offset(i)
if ascii_number < 97
ascii_number += 26
end
placeholder << ascii_number.chr
i += 1
end
puts placeholder.gsub("stop","\n")
end
def offset(num)
num = num % @offset_str.length
@offset_str[num].to_num - 97
end
end
encrpyted_message = "eceueawimetkvpgztloxqbrrghfhywfyriievgzqzmsqtksqyzbsggkccnrkzsuobuhwwadw"\
"vfxrghuiisftrdciwgsndiscausejothpsoqvtlbhedxlphlfrswcqxhgzvoihvnjqpsrf"\
"ppoksffrfuleyvoprfrnrfultjpthpgjoiwgxyhpxswchrkiohrgkpwesgyvwyaeuaqbvv"\
"hngffhskevsvbrlhjfrtjygsyiadusexwguvmjsieqgusphvnzfkghhdbxhvvvvvqnvhsox"\
"rcuesrbqpucdiefppgzrrtksdevgmqfrpbuwtjjtglpazbhthgvrtksaclqbmhhphaaskuy"\
"hftrdulepwtctsrdxdustjljwurrnecyetpvthyhuiuhzxwqmktkmsiysgsutowbufrdd"\
"bexhtlgkvggoqsiynfygrjwkthsoilgcgbwchrkiohrgkcbusvgkhfinuaqdxcbdoido"
puts "Please enter the key"
key = gets.chomp
puts "The key '#{key}' has decoded the message as follows:"
d = Decoder.new(encrpyted_message, key)
d.decode