Class: ENIGMA::PossibleKeys

Inherits:
Object
  • Object
show all
Includes:
EnigmaHelpers
Defined in:
lib/enigma/possible_keys.rb

Instance Method Summary collapse

Methods included from EnigmaHelpers

#character_map, #date_of_encryption, #format_month, #generate_key, #key_rotation, #offset_key, #offset_rotation, #total_rotation

Constructor Details

#initialize(file_name, tday) ⇒ PossibleKeys

Returns a new instance of PossibleKeys.



7
8
9
10
11
12
13
14
15
# File 'lib/enigma/possible_keys.rb', line 7

def initialize(file_name, tday)
  @tday = tday
  @file_name = file_name
  @read_write = Files.new
  @count = 0
  @file_crack = file_to_crack
  @last_four = last_four_characters
  @character_map = character_map
end

Instance Method Details

#crack_helper?(plain_char, encrypted_char, test_value) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/enigma/possible_keys.rb', line 37

def crack_helper?(plain_char, encrypted_char, test_value)
  @character_map[crack_test(plain_char, test_value)] == encrypted_char
end

#crack_test(plain_char, test_value) ⇒ Object



41
42
43
# File 'lib/enigma/possible_keys.rb', line 41

def crack_test(plain_char, test_value)
  (@character_map.index(plain_char.chr) + (date_offset[key_position(@count)].to_i + test_value)) % @character_map.size
end

#date_offsetObject



62
63
64
# File 'lib/enigma/possible_keys.rb', line 62

def date_offset
  offset_key(@tday)
end

#file_to_crackObject



45
46
47
# File 'lib/enigma/possible_keys.rb', line 45

def file_to_crack
  @file_crack ||= @read_write.read_file(@file_name).chomp
end

#find_rotation(_encrypted_char) ⇒ Object



49
50
51
52
# File 'lib/enigma/possible_keys.rb', line 49

def find_rotation(_encrypted_char)
  total_length = @file_crack.length - 1
  ((total_length - 3) + @last_four.index(@last_four[@count])) % 4
end

#get_partial_key(weakness) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/enigma/possible_keys.rb', line 17

def get_partial_key(weakness)
  @partial_key = []
  weakness.each_byte do |each_char|
    @partial_key[key_position(@count)] ||= []
    @partial_key[key_position(@count)] = sub_key(each_char, @last_four[@count])
    @count += 1
  end
  @partial_key
end

#key_position(count) ⇒ Object



54
55
56
# File 'lib/enigma/possible_keys.rb', line 54

def key_position(count)
  find_rotation(@last_four[count])
end

#last_four_charactersObject



58
59
60
# File 'lib/enigma/possible_keys.rb', line 58

def last_four_characters
  @file_crack.split("").last(4).join
end

#sub_key(plain_char, encrypted_char) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/enigma/possible_keys.rb', line 27

def sub_key(plain_char, encrypted_char)
  sub_array = []
  10.times do |i|
    10.times do |j|
      sub_array << "#{i}#{j}" if crack_helper?(plain_char, encrypted_char, "#{i}#{j}".to_i)
    end
  end
  sub_array
end