Class: Krypton::AESCrypt

Inherits:
Object
  • Object
show all
Defined in:
lib/core/aes.rb

Class Method Summary collapse

Class Method Details

.decrypt(message, password, outfile = '') ⇒ Object



14
15
16
17
18
19
# File 'lib/core/aes.rb', line 14

def self.decrypt(message, password, outfile='')
  base64_decoded = Base64.decode64(message.to_s.strip)
  d = self.decrypt_data(base64_decoded, self.key_digest(password), nil, "AES-256-CBC")
  File.write(outfile, d) unless outfile == '' || outfile.nil?
  d
end

.decrypt_data(encrypted_data, key, iv, cipher_type) ⇒ Object

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 :arg: encrypted_data => String :arg: key => String :arg: iv => String :arg: cipher_type => String



36
37
38
39
40
41
42
# File 'lib/core/aes.rb', line 36

def self.decrypt_data(encrypted_data, key, iv, cipher_type)
  aes = 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, outfile = '') ⇒ Object

Taken from github.com/Gurpartap/aescrypt/blob/master/lib/aescrypt.rb and improved for newew ruby versions



8
9
10
11
12
# File 'lib/core/aes.rb', line 8

def self.encrypt(message, password, outfile='')
  a = Base64.encode64(self.encrypt_data(message.to_s.strip, self.key_digest(password), nil, "AES-256-CBC"))
  File.write(outfile, a) unless outfile == '' || outfile.nil?
  a
end

.encrypt_data(data, key, iv, cipher_type) ⇒ Object

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 :arg: data => String :arg: key => String :arg: iv => String :arg: cipher_type => String



55
56
57
58
59
60
61
# File 'lib/core/aes.rb', line 55

def self.encrypt_data(data, key, iv, cipher_type)
  aes = 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) ⇒ Object



21
22
23
# File 'lib/core/aes.rb', line 21

def self.key_digest(password)
  OpenSSL::Digest::SHA256.new(password).digest
end