Class: EnigmaEngine::Crack
Instance Attribute Summary
Attributes inherited from Engine
Instance Method Summary collapse
- #crack(encrypted_file, new_file, date) ⇒ Object
- #find_key(encrypted_file) ⇒ Object
- #get_target(encrypted_file) ⇒ Object
- #handle_crack_rotation(index, item, res, func) ⇒ Object
- #handle_get_target(encrypted_file, res) ⇒ Object
Methods inherited from Engine
#characters, #decrypt, #decrypt_char, #encrypt, #encrypt_char, #handle_rotation, #rand_key
Methods included from DateKeyHelpers
#blue, #colorize, #date_valid?, #fields_empty?, #files_valid?, #green, #key_valid?, #red, #today, #yellow
Methods included from FileHelpers
#create_write_file, #open_file, #show_msg, #text_to_array
Methods included from Offsets
#a_offset, #b_offset, #c_offset, #d_offset, #get_date
Methods included from Rotors
#a_rotation, #b_rotation, #c_rotation, #d_rotation, #key, #put_key
Instance Method Details
#crack(encrypted_file, new_file, date) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/enigma_engine/crack.rb', line 49 def crack(encrypted_file, new_file, date) res = find_key(encrypted_file) key = res.values.map(&:to_s).join('') encrypted_file = text_to_array(open_file(encrypted_file)) cracked_chars = [] encrypted_file.each do |row| row.each_with_index do |item, index| brk = handle_crack_rotation(index, item, res, method(:decrypt_char)) cracked_chars.push(brk) end end create_write_file(cracked_chars.join(''), new_file, key, date) end |
#find_key(encrypted_file) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/enigma_engine/crack.rb', line 21 def find_key(encrypted_file) encrypted_file = text_to_array(open_file(encrypted_file)) char_map_with_index = Hash[characters.zip (0...characters.size)] target_char = get_target(encrypted_file) rotation = {} target_char[:target].each_with_index do |item, index| weakness_pos = char_map_with_index[target_char[:weakness][index]] pos = char_map_with_index[item] - weakness_pos (pos < 0) ? pos += 39 : pos case index when 0 then rotation[:a_rotation] = pos when 1 then rotation[:b_rotation] = pos when 2 then rotation[:c_rotation] = pos when 3 then rotation[:d_rotation] = pos end end rotation end |
#get_target(encrypted_file) ⇒ Object
15 16 17 18 19 |
# File 'lib/enigma_engine/crack.rb', line 15 def get_target(encrypted_file) res = {} res[:target] = encrypted_file[encrypted_file.length - 2] handle_get_target(encrypted_file, res) end |
#handle_crack_rotation(index, item, res, func) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/enigma_engine/crack.rb', line 40 def handle_crack_rotation(index, item, res, func) case index when 0 then func.call(item, res[:a_rotation]) when 1 then func.call(item, res[:b_rotation]) when 2 then func.call(item, res[:c_rotation]) when 3 then func.call(item, res[:d_rotation]) end end |
#handle_get_target(encrypted_file, res) ⇒ Object
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/enigma_engine/crack.rb', line 4 def handle_get_target(encrypted_file, res) case encrypted_file.last.length when 4 then res[:target] = encrypted_file.last res[:weakness] = %w(n d . .) when 3 then res[:weakness] = %w(. . e n) when 2 then res[:weakness] = %w(. e n d) when 1 then res[:weakness] = %w(e n d .) end res end |