module AESCrypt

Public Class Methods

decrypt(message, password) click to toggle source
# File app/lib/aescrypt.rb, line 36
def self.decrypt(message, password)
  base64_decoded = Base64.decode64(message.to_s.strip)
  self.decrypt_data(base64_decoded, self.key_digest(password), nil, "AES-256-CBC")
end
decrypt_data(cipher_type => String) click to toggle source
Decrypts a block of data (encrypted_data) given an encryption key
and an initialization vector (iv).  Keys, iv's, and the data
returned are all binary strings.  Cipher_type should be
"AES-256-CBC", "AES-256-ECB", or any of the cipher types
supported by OpenSSL.  Pass nil for the iv if the encryption type
doesn't use iv's (like ECB).

:return: => String

# File app/lib/aescrypt.rb, line 56
def self.decrypt_data(encrypted_data, key, iv, cipher_type)
  aes = RBCM::OpenSSL::Cipher.new(cipher_type)
  aes.decrypt
  aes.key = key
  aes.iv = iv if iv != nil
  aes.update(encrypted_data) + aes.final
end
encrypt(message, password) click to toggle source
# File app/lib/aescrypt.rb, line 32
def self.encrypt(message, password)
  Base64.encode64(self.encrypt_data(message.to_s.strip, self.key_digest(password), nil, "AES-256-CBC")).chomp
end
encrypt_data(cipher_type => String) click to toggle source
Encrypts a block of data given an encryption key and an
initialization vector (iv).  Keys, iv's, and the data returned
are all binary strings.  Cipher_type should be "AES-256-CBC",
"AES-256-ECB", or any of the cipher types supported by OpenSSL.
Pass nil for the iv if the encryption type doesn't use iv's (like
ECB).

:return: => String

# File app/lib/aescrypt.rb, line 75
def self.encrypt_data(data, key, iv, cipher_type)
  aes = RBCM::OpenSSL::Cipher.new(cipher_type)
  aes.encrypt
  aes.key = key
  aes.iv = iv if iv != nil
  aes.update(data) + aes.final
end
key_digest(password) click to toggle source
# File app/lib/aescrypt.rb, line 41
def self.key_digest(password)
  OpenSSL::Digest::SHA256.new(password).digest
end