This repository has been archived by the owner on Oct 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enigma.rb
107 lines (95 loc) · 2.85 KB
/
enigma.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
require_relative 'machine'
require_relative 'plugboard'
require_relative 'rotor'
require_relative 'reflector'
class Enigma
def call
puts "Welcome to the Enigma Machine"
run
end
def get_user_input
gets.chomp.strip
end
def run
print "Please select an option: "
input = get_user_input
case input
when "encrypt"
encrypt
when "decrypt"
decrypt
when "generate rotor"
generate_rotor
when "generate reflector"
generate_reflector
when "generate plugboard"
generate_plugboard
when "help"
help
when "exit"
exit
else
puts "Invalid input"
end
end
def encrypt
my_enigma = Machine.new
print "Input rotors to use in the following format (1 2 5): "
rotors = get_user_input.split
print "Input rotor offsets in the following format (21 3 11): "
offsets = get_user_input.split
my_enigma.rotor_fast = Rotor.new(rotors[0].to_i, offsets[0].to_i)
my_enigma.rotor_medium = Rotor.new(rotors[1].to_i, offsets[1].to_i)
my_enigma.rotor_slow = Rotor.new(rotors[2].to_i, offsets[2].to_i)
print "Input phrase to encrypt: "
input = get_user_input.upcase
puts "Your encrypted phrase is: #{my_enigma.encrypt(input)}"
end
def decrypt
my_enigma = Machine.new
print "Input rotors to use in the following format (1 2 5): "
rotors = get_user_input.split
print "Input rotor offsets in the following format (21 3 11): "
offsets = get_user_input.split
my_enigma.rotor_fast = Rotor.new(rotors[0].to_i, offsets[0].to_i)
my_enigma.rotor_medium = Rotor.new(rotors[1].to_i, offsets[1].to_i)
my_enigma.rotor_slow = Rotor.new(rotors[2].to_i, offsets[2].to_i)
print "Input phrase to decrypt: "
input = get_user_input.upcase
puts "Your decrypted phrase is: #{my_enigma.encrypt(input)}"
end
def generate_rotor
print "Select which rotor to generate (1-5). Type 'all' to generate all rotors: "
input = get_user_input
if input.is_a? Fixnum
Rotor.generate_rotor(input)
puts "Rotor successfully generated"
elsif input == "all"
Rotor.generate_rotor(1)
Rotor.generate_rotor(2)
Rotor.generate_rotor(3)
Rotor.generate_rotor(4)
Rotor.generate_rotor(5)
puts "Rotors successfully generated"
else
puts "Invalid input"
end
end
def generate_reflector
Reflector.generate_reflector
puts "Reflector generated"
end
def generate_plugboard
Plugboard.generate_plugboard
puts "Plugboard generated"
end
def help
puts "Type 'encrypt' to encrypt your message"
puts "Type 'decrypt' to decrypt your message"
puts "Type 'generate rotor' followed by a number (1-5) to generate that rotor"
puts "Type 'generate reflector' to generate a reflector"
puts "Type 'exit' to exit"
puts "Type 'help' to view this menu again"
end
end
Enigma.new.call