class EDfile

Public Class Methods

decrypt(opts = {}) click to toggle source
# File lib/encrypter_decrypter/decrypt.rb, line 3
 def self.decrypt(opts = {})
  obj = new(opts)
  obj.trigger_decryption
end
encrypt(opts = {}) click to toggle source
# File lib/encrypter_decrypter/encrypt.rb, line 5
 def self.encrypt(opts = {})
  obj = new(opts)
  obj.trigger_encryption
end

Public Instance Methods

trigger_decryption() click to toggle source
# File lib/encrypter_decrypter/decrypt.rb, line 8
def trigger_decryption

  original_file_name = File.basename(@path.split('.enc').first)
  data = YAML.load_file('iv_key.yml')
  key = data[original_file_name][:key]
  iv = data[original_file_name][:iv]

  cipher = OpenSSL::Cipher.new("aes-#{@key_size}-cbc")
  cipher.decrypt
  cipher.key = key
  cipher.iv = iv

  buf = ""
  original_file = @path.split('.enc').first
  #encrypted_file = @path
  File.open(original_file, "wb") do |outf|
  File.open(@path, "rb") do |inf|
    while inf.read(4096, buf)
      outf << cipher.update(buf)
    end
    outf << cipher.final
  end
 end

  ap 'Holla, Decrypted!'
end
trigger_encryption() click to toggle source
# File lib/encrypter_decrypter/encrypt.rb, line 10
def trigger_encryption
  cipher = OpenSSL::Cipher.new("aes-#{@key_size}-cbc")
  cipher.encrypt
  key = cipher.random_key
  iv = cipher.random_iv

  buf = ""
  #original_file = File.basename(@path)
  #encrypted_file = original_file + '.enc'
  encrypted_file = @path + '.enc'
  File.open(encrypted_file, "wb") do |outf|
    File.open(@path, "rb") do |inf|
      while inf.read(4096, buf)
        outf << cipher.update(buf)
      end
      outf << cipher.final
    end
  end
  write('iv_key.yml',{"#{File.basename(@path)}" => {iv: iv, key: key}})
  
end