class Ratsel::Encrypt

Attributes

character_map[R]
encrypted_txt[R]
encryption_date[R]
encryption_key[R]
message_txt[R]
offsets_array[R]
rotation_array[R]
sum_rotation_offset[R]

Public Class Methods

new(message_txt, encrypted_txt) click to toggle source
# File lib/ratsel/encrypt.rb, line 12
def initialize(message_txt, encrypted_txt)
  @message_txt = message_txt
  @encrypted_txt = encrypted_txt
  @encryption_date = Time.now.strftime("%d%m%y")
  @character_map = Cipher.character_map
  @encryption_key = (1..5).map { (1..9).to_a[rand(9)] }.join
  @offsets_array = Helpers.offsets_array(@encryption_date)
  @rotation_array = Helpers.rotation_array(@encryption_key)
  @sum_rotation_offset = Helpers.sum_rotation_offset(@offsets_array, @rotation_array)
end

Public Instance Methods

encrypt() click to toggle source
# File lib/ratsel/encrypt.rb, line 23
def encrypt
  begin
    message = Accessor.read_file_text(@message_txt)
    encrypted_message = ''
    sliced_message_array = []

    message.each_slice(4) { |message|
      sliced_message_array << message.join('')
    }

    sliced_message_array.each_with_index { |batch, index|
      message_batch = batch.downcase.split('')
      n = message_batch.length
  
      n.times { |i|
        char_rotate = @character_map.rotate(@character_map.index(message_batch.shift))
        rotated_array = char_rotate.rotate(@sum_rotation_offset[i])
        encrypted_message << rotated_array.shift
      }
    }

    message(encrypted_txt, encryption_key, encryption_date)

    Accessor.write_encrypted_texts(encrypted_txt, encrypted_message)

    encrypted_message

  rescue TypeError => e
    puts "#{e}\nMake sure the characters in your text file are numbers, letters, a period, space or comma."
  end
end