Class: Decryption

Inherits:
Object
  • Object
show all
Includes:
EnigmaHelpers
Defined in:
lib/enigma/decryption.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(encrypted_file, plain_file, key, offset_key) ⇒ Decryption

Returns a new instance of Decryption.



5
6
7
8
9
10
11
12
13
14
# File 'lib/enigma/decryption.rb', line 5

def initialize(encrypted_file, plain_file, key, offset_key)
  @encrypted_file = encrypted_file
  @plain_file = plain_file
  @key = key
  @read_write = Files.new
  @offset_key = offset_key
  @count = 0
  @text = ""
  @character_map = character_map
end

Instance Method Details

#decrypt(encrypted_text) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/enigma/decryption.rb', line 16

def decrypt(encrypted_text)
  encrypted_text.each_byte do |each_char|
    @text << @character_map[decrypt_helper(each_char, @count)]
    @count += 1
  end
  @text
end

#decrypt_helper(each_char, _count) ⇒ Object



24
25
26
# File 'lib/enigma/decryption.rb', line 24

def decrypt_helper(each_char, _count)
  (@character_map.index(each_char.chr) - get_total_rotation) % @character_map.size
end

#decrypt_writeObject



32
33
34
# File 'lib/enigma/decryption.rb', line 32

def decrypt_write
  @read_write.write_file(@plain_file, decrypt(file_to_decript))
end

#file_to_decriptObject



36
37
38
# File 'lib/enigma/decryption.rb', line 36

def file_to_decript
  @read_write.read_file(@encrypted_file).chomp
end

#get_total_rotationObject



28
29
30
# File 'lib/enigma/decryption.rb', line 28

def get_total_rotation
  total_rotation(@count, @key, offset_key(date_of_encryption))
end