class MiniEnigma

Public Class Methods

configure(key, iv) click to toggle source
# File lib/minienigma.rb, line 8
def self.configure(key, iv)
        @@key = key
        @@iv = iv
end
decrypt(data) click to toggle source
# File lib/minienigma.rb, line 32
      def self.decrypt(data)
              self.validate_config
  begin
    decipher = OpenSSL::Cipher::AES.new(256, :CBC)
    decipher.decrypt
    decipher.key = @@key
    decipher.iv = @@iv
    plain_data = decipher.update(Base64.decode64(data)) + decipher.final
    return plain_data.force_encoding('UTF-8')
  rescue Exception => e
    puts e.backtrace
    return nil
  end
end
encrypt(data) click to toggle source
# File lib/minienigma.rb, line 21
      def self.encrypt(data)
              self.validate_config
              raise "Data to encrypt cannot be empty" if (data == nil or data.empty?)
  cipher = OpenSSL::Cipher::AES.new(256, :CBC)
  cipher.encrypt
  cipher.key = @@key
  cipher.iv = @@iv
  encrypted_data = cipher.update(data) + cipher.final
  return Base64.encode64(encrypted_data).encode('utf-8')
end
keys() click to toggle source
# File lib/minienigma.rb, line 13
def self.keys
        return "Key: #{@@key} - IV: #{@@iv}"
end
url_encode(data) click to toggle source
# File lib/minienigma.rb, line 17
def self.url_encode(data)
  return URI.encode_www_form_component(data)
end

Private Class Methods

validate_config() click to toggle source
# File lib/minienigma.rb, line 48
def self.validate_config
              raise "Invalid Key, it must be 32 characters long" if (@@key == nil or @@key.length != 32)
              raise "Invalid IV, it must be 16 characters long" if (@@iv == nil or @@iv.length != 16)
      end