class Rijndael::Base

Simply encrypt and decrypt strings using Rijndael.

Public Class Methods

cipher() click to toggle source
# File lib/rijndael/base.rb, line 81
def self.cipher
  @cipher ||= OpenSSL::Cipher.new 'aes-256-cbc'
end
generate_key() click to toggle source

Generate a random key for encryption.

@return [String] Encryption key.

# File lib/rijndael/base.rb, line 77
def self.generate_key
  Base64.strict_encode64(cipher.random_key)
end
new(key) click to toggle source

This constructor sets the de-/encryption key.

@param key [String] Encryption key.

# File lib/rijndael/base.rb, line 21
def initialize(key)
  @key = key
  fail ArgumentError, 'Key is empty.' if key.nil? || key.empty?
end

Public Instance Methods

decrypt(encrypted) click to toggle source

This method expects a base64 encoded cipher text and decrypts it.

@param encrypted [String] Cipher text.

@return [String] Plain text.

# File lib/rijndael/base.rb, line 56
def decrypt(encrypted)
  fail ArgumentError, 'No cipher text supplied.' if encrypted.nil? || encrypted.empty?

  matches = CIPHER_PATTERN.match(encrypted)

  fail ArgumentError, 'Cipher text has an unsupported format.' if matches.nil?

  cipher = self.class.cipher
  cipher.decrypt
  cipher.key = Base64.decode64(@key)
  cipher.iv = Base64.decode64(matches[1])
  decrypted = cipher.update(Base64.decode64(matches[2]))

  decrypted << cipher.final
end
encrypt(plain) click to toggle source

This method expects a plain text of arbitrary length and encrypts it.

@param plain [String] Plain text.

@return [String] Cipher text.

# File lib/rijndael/base.rb, line 33
def encrypt(plain)
  fail ArgumentError, 'No plain text supplied.' if plain.nil? || plain.empty?

  cipher = self.class.cipher
  cipher.encrypt
  cipher.key = Base64.decode64(@key)
  cipher.iv = iv = cipher.random_iv
  encrypted = cipher.update(plain)
  encrypted << cipher.final

  iv = Base64.strict_encode64(iv)
  encrypted = Base64.strict_encode64(encrypted)

  "$#{iv}$#{encrypted}"
end