Class: Gibberish::AES

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

Overview

Handles AES encryption and decryption in a way that is compatible with OpenSSL.

Defaults to 256-bit CBC encryption, ideally you should leave it this way

Basic Usage

Encrypting

cipher = Gibberish::AES.new('p4ssw0rd')
cipher.encrypt("some secret text")
#=> "U2FsdGVkX1/D7z2azGmmQELbMNJV/n9T/9j2iBPy2AM=\n"
cipher.encrypt_file("secret.txt", "secret.txt.enc")

Decrypting

cipher = Gibberish::AES.new('p4ssw0rd')
cipher.decrypt(""U2FsdGVkX1/D7z2azGmmQELbMNJV/n9T/9j2iBPy2AM=\n"")
#=> "some secret text"
cipher.decrypt_file("secret.txt.enc", "secret.txt")

OpenSSL Interop

echo "U2FsdGVkX1/D7z2azGmmQELbMNJV/n9T/9j2iBPy2AM=\n" | openssl enc -d -aes-256-cbc -a -k p4ssw0rd
openssl aes-256-cbc -d -in secret.txt.enc -out secret.txt -k p4ssw0rd

Constant Summary collapse

BUFFER_SIZE =
4096

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(password, size = 256, mode = "cbc") ⇒ AES

Initialize with the password

Parameters:

  • password (String)
  • size (Integer) (defaults to: 256)
  • mode (String) (defaults to: "cbc")


40
41
42
43
44
45
# File 'lib/gibberish/aes.rb', line 40

def initialize(password, size=256, mode="cbc")
  @password = password
  @size = size
  @mode = mode
  @cipher = OpenSSL::Cipher::Cipher.new("aes-#{size}-#{mode}")
end

Instance Attribute Details

#cipherObject (readonly)

Returns the value of attribute cipher.



33
34
35
# File 'lib/gibberish/aes.rb', line 33

def cipher
  @cipher
end

#passwordObject (readonly)

Returns the value of attribute password.



33
34
35
# File 'lib/gibberish/aes.rb', line 33

def password
  @password
end

#sizeObject (readonly)

Returns the value of attribute size.



33
34
35
# File 'lib/gibberish/aes.rb', line 33

def size
  @size
end

Instance Method Details

#decrypt(data, opts = {}) ⇒ Object Also known as: dec, d

Raises:

  • (ArgumentError)


57
58
59
60
61
62
63
64
# File 'lib/gibberish/aes.rb', line 57

def decrypt(data, opts={})
  raise ArgumentError, 'Data is too short' unless data.length >= 16
  data = Base64.decode64(data) unless opts[:binary]
  salt = data[8..15]
  data = data[16..-1]
  setup_cipher(:decrypt, salt)
  cipher.update(data) + cipher.final
end

#decrypt_file(from_file, to_file) ⇒ Object Also known as: dec_file, df



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/gibberish/aes.rb', line 85

def decrypt_file(from_file, to_file)
  buf = ""
  salt = ""
  File.open(to_file, "wb") do |outf|
    File.open(from_file, "rb") do |inf|
      inf.seek(8, IO::SEEK_SET)
      inf.read(8, salt)
      setup_cipher(:decrypt, salt)
      while inf.read(4096, buf)
        outf << self.cipher.update(buf)
      end
      outf << self.cipher.final
    end
  end
end

#decrypt_stream(in_stream, out_stream) ⇒ Object



110
111
112
113
114
115
# File 'lib/gibberish/aes.rb', line 110

def decrypt_stream(in_stream, out_stream)
  header = in_stream.read(16)
  salt = header[8..15]
  setup_cipher(:decrypt, salt)
  copy_stream in_stream, out_stream
end

#encrypt(data, opts = {}) ⇒ Object Also known as: enc, e



47
48
49
50
51
52
53
# File 'lib/gibberish/aes.rb', line 47

def encrypt(data, opts={})
  salt = generate_salt(opts[:salt])
  setup_cipher(:encrypt, salt)
  e = cipher.update(data) + cipher.final
  e = "Salted__#{salt}#{e}" #OpenSSL compatible
  opts[:binary] ? e : Base64.encode64(e)
end

#encrypt_file(from_file, to_file, opts = {}) ⇒ Object Also known as: enc_file, ef



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/gibberish/aes.rb', line 68

def encrypt_file(from_file, to_file, opts={})
  salt = generate_salt(opts[:salt])
  setup_cipher(:encrypt, salt)
  buf = ""
  File.open(to_file, "wb") do |outf|
    outf << "Salted__#{salt}"
    File.open(from_file, "rb") do |inf|
      while inf.read(4096, buf)
        outf << self.cipher.update(buf)
      end
      outf << self.cipher.final
    end
  end
end

#encrypt_stream(in_stream, out_stream, opts = {}) ⇒ Object



103
104
105
106
107
108
# File 'lib/gibberish/aes.rb', line 103

def encrypt_stream(in_stream, out_stream, opts={})
  salt = generate_salt(opts[:salt])
  setup_cipher(:encrypt, salt)
  out_stream << "Salted__#{salt}"
  copy_stream in_stream, out_stream
end