class Enygma::Encryptor

Attributes

cypher_filename[R]
encrypted[R]
encryption_date[R]
encryption_key[R]
filename[R]
offset[R]

Public Class Methods

new(filename, cypher_filename = nil) click to toggle source
# File lib/enygma/encryptor.rb, line 13
def initialize(filename, cypher_filename = nil)
  @filename = filename
  @cypher_filename = cypher_filename
  @encryption_key =  rand(10**5).to_s.rjust(5, '0')
  @encryption_date = Time.now.strftime("%d%m%y")
  @offset = Offset.get_offset(@encryption_date)
  @encrypted = ""
end

Public Instance Methods

encrypt() click to toggle source
# File lib/enygma/encryptor.rb, line 22
def encrypt
  begin
    plain_characters = Filer.read(@filename)
    plain_characters.each_slice(4) { |batch| encrypt_batch(batch) }
    @cypher_filename = Filer.write(
      @cypher_filename,
      @encrypted,
      @filename,
      "encrypted"
    )

    show_confirmation_message(
      @cypher_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
encrypt_batch(batch) click to toggle source
# File lib/enygma/encryptor.rb, line 44
def encrypt_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 }

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