This repository has been archived by the owner on May 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
krypton.rb
executable file
·130 lines (107 loc) · 2.6 KB
/
krypton.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env ruby
require "base64"
require "readline"
class Krypton
def main(level)
if !level.nil? && level.to_i >= 1 && level.to_i <= 7 then
method = "level" + level
puts "Level %d" % level
puts
if self.respond_to?(method) then
self.method(method).call
else
puts "Not implemented yet"
end
else
puts "Level must be between 1-7"
end
end
def level1
print "Enter Base64 encoded password: "
cipher = $stdin.gets
text = Base64.decode64(cipher)
puts "Decrypted text: %s" % text
end
def level2
print "Enter ciphertext: "
cipher = $stdin.gets
text = cipher.tr("a-mA-Mn-zN-Z", "n-zN-Za-mA-M")
puts "Decrypted text: %s" % text
end
def level3
print "Enter ciphertext: "
cipher = $stdin.gets
text = cipher.tr("a-lA-Lm-zM-Z", "o-zO-Za-nA-N")
puts "Decrypted text: %s" % text
end
def level4
letters = {}
english = [
"E", "T", "A", "O", "I", "N", "S", "H", "R", "D", "L", "U", "C",
"M", "W", "F", "Y", "G", "P", "B", "V", "K", "X", "J", "Q", "Z",
]
puts "Enter intercepted messages (empty line ends input)"
loop do
print ": "
line = $stdin.gets
line.strip!
break if line.empty?
line.upcase!
line.each_char do |letter|
next unless letter.match?(/[A-Z]/)
letters[letter] = 0 if letters[letter].nil?
letters[letter] += 1
end
end
puts
letters = letters.sort_by {|k, v| -v}
letters.each_index do |i|
printf "%s | %03d | " % [letters[i][0], letters[i][1]]
unless english[i].nil? then
print "%s" % english[i]
end
puts
end
dictionary = []
letters.each_index do |i|
dictionary[i] = letters[i][0]
end
puts
print "Enter ciphertext: "
cipher = $stdin.gets
text = cipher.tr(dictionary.join, english.join)
puts "Decrypted text: %s" % text
end
end
krypton = Krypton.new
case ARGV[0]
when "-h"
puts "Usage: #{__FILE__} [-l <num>]"
puts " -l <num> Execute level"
puts " -h This help"
exit
when "-l"
krypton.main(ARGV[1])
exit
end
loop do
cmds = Readline.readline("Krypton> ", true)
next if cmds.nil?
cmds.strip!
next if cmds.empty?
cmds.downcase!
cmds = cmds.split(/\s+/)
case cmds[0]
when /^q(uit)?$/
exit
when /^h(elp)?$/, "?"
puts "Available commands:"
puts " l(evel) <num> Execute level"
puts " h(elp) This help"
puts " q(uit) Quit"
when /^l(evel)?$/
krypton.main(cmds[1])
else
puts "Unrecognized command"
end
end