Class: Enygma::Decryptor

Inherits:
Object
  • Object
show all
Includes:
Confirmation
Defined in:
lib/enygma/decryptor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Confirmation

#show_confirmation_message

Constructor Details

#initialize(cypher_filename, encryption_key, encryption_date, plain_filename = nil) ⇒ Decryptor

Returns a new instance of Decryptor.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/enygma/decryptor.rb', line 13

def initialize(
  cypher_filename,
  encryption_key,
  encryption_date,
  plain_filename = nil
)
  @cypher_filename = cypher_filename
  @plain_filename = plain_filename
  @encryption_key =  encryption_key
  @encryption_date = encryption_date
  @offset = Offset.get_offset(@encryption_date)
  @decrypted = ""
end

Instance Attribute Details

#cypher_filenameObject (readonly)

Returns the value of attribute cypher_filename.



10
11
12
# File 'lib/enygma/decryptor.rb', line 10

def cypher_filename
  @cypher_filename
end

#decryptedObject (readonly)

Returns the value of attribute decrypted.



10
11
12
# File 'lib/enygma/decryptor.rb', line 10

def decrypted
  @decrypted
end

#encryption_dateObject (readonly)

Returns the value of attribute encryption_date.



10
11
12
# File 'lib/enygma/decryptor.rb', line 10

def encryption_date
  @encryption_date
end

#encryption_keyObject (readonly)

Returns the value of attribute encryption_key.



10
11
12
# File 'lib/enygma/decryptor.rb', line 10

def encryption_key
  @encryption_key
end

#plain_filenameObject (readonly)

Returns the value of attribute plain_filename.



10
11
12
# File 'lib/enygma/decryptor.rb', line 10

def plain_filename
  @plain_filename
end

Instance Method Details

#decryptObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/enygma/decryptor.rb', line 27

def decrypt
  begin
    cypher_characters = Filer.read(@cypher_filename)
    cypher_characters.each_slice(4) { |batch| decrypt_batch(batch) }
    @plain_filename = Filer.write(
      @plain_filename,
      @decrypted,
      @cypher_filename,
      "decrypted"
    )

    show_confirmation_message(
      @plain_filename,
      @encryption_key,
      @encryption_date
    )
  rescue StandardError => e
    puts "Could not open the file you supplied."\
    "Make sure you are correctly typing the correct path #{e.message}"
  end
end

#decrypt_batch(batch) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/enygma/decryptor.rb', line 49

def decrypt_batch(batch)
  key_characters = @encryption_key.split("")
  offset_characters = @offset.split("")
  batch.each_with_index do |_value, index|
    new_index = Rotator.rotate(
      key_characters[index],
      key_characters[index + 1],
      offset_characters[index],
      batch[index]
    ) { |x, y| x - y }

    @decrypted += Enygma::CHARACTER_MAP[new_index]
  end
end