Class: Cript::Simple

Inherits:
Cripter show all
Defined in:
lib/cript/simple.rb

Overview

Cript::Simple is a simple ruby wrapper around RSA SSH keys and a blowfish cipher. It allows you to easily encrypt and decrypt strings.

Instance Method Summary collapse

Methods inherited from Cripter

#echo, #initialize, #inspect

Constructor Details

This class inherits a constructor from Cript::Cripter

Instance Method Details

#decrypt(message) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/cript/simple.rb', line 21

def decrypt(message)
  encrypted_key, iv, encrypted_message = Marshal.load(Base64::decode64(message))
  key = @private_key.private_decrypt(encrypted_key)
  cipher = OpenSSL::Cipher::Cipher.new('bf-cbc').decrypt
  cipher.key = key
  cipher.iv = iv
  cipher.update(encrypted_message) + cipher.final
end

#encrypt(message) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/cript/simple.rb', line 12

def encrypt(message)
  cipher = OpenSSL::Cipher::Cipher.new('bf-cbc').encrypt
  key = cipher.random_key
  iv = cipher.random_iv
  encrypted_message = cipher.update(message) + cipher.final
  encrypted_key = @public_key.public_encrypt(key)
  Base64::encode64(Marshal.dump([encrypted_key,iv,encrypted_message]))
end