Class: WIKK::AES_256

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

Overview

Provides AES 256 Encryption, as well as generation of keys and initial vectors, which could be used in other places.

Constant Summary collapse

VERSION =
'0.1.8'
AES_256_CBC =
'AES-256-CBC'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_string = nil, iv_string = nil) ⇒ AES_256

Initialize

Parameters:

  • key_string (String) (defaults to: nil)

    optional base64 key to be used in encryption or decryption. if nil, then key and iv are generated automatically. Recover the key with key_to_s(), or key_iv_to_s()

  • iv_string (String) (defaults to: nil)

    optional base64 iv (initial vector) to be used in the encryption or decryption Overwritten by auto generated iv, if key_string is nil. Recover with iv_to_str() or key_iv_to_s().



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/wikk_aes_256.rb', line 24

def initialize(key_string = nil, iv_string = nil)
  if key_string.nil?
    gen_key(key_length: 32)
  else
    str_to_key(key_string)
  end

  if iv_string.nil?
    gen_iv
  else
    str_to_iv(iv_string)
  end
end

Instance Attribute Details

#cipher_textString (readonly)

the encrypted text

Returns:

  • (String)

    the current value of cipher_text



12
13
14
# File 'lib/wikk_aes_256.rb', line 12

def cipher_text
  @cipher_text
end

#plain_textString (readonly)

the decrypted text

Returns:

  • (String)

    the current value of plain_text



12
13
14
# File 'lib/wikk_aes_256.rb', line 12

def plain_text
  @plain_text
end

Class Method Details

.cipher_to_s(unencrypted_source, key_string = nil, iv_string = nil) ⇒ String

Converts encrypted source String, @cipher_text, into Base64 String

Parameters:

  • unencrypted_source (String|File)

    which must be present, as AES_256 class is created here.

  • key_string (String) (defaults to: nil)

    optional base64 key to be used in encryption or decryption. if nil, then key and iv are generated automatically. Recover the key with key_to_s(), or key_iv_to_s()

  • iv_string (String) (defaults to: nil)

    optional base64 iv (initial vector) to be used in the encryption or decryption Overwritten by auto generated iv, if key_string is nil. Recover with iv_to_str() or key_iv_to_s().

Returns:

  • (String, String, String)

    Base64 string representing encrypted source; base64 key, @key, so later decryption can be done; base64 initial vector, @iv, so later decryption can be done



180
181
182
183
# File 'lib/wikk_aes_256.rb', line 180

def self.cipher_to_s(unencrypted_source, key_string = nil, iv_string = nil)
  aes = self.new(key_string, iv_string)
  return aes.cipher_to_s(unencrypted_source), aes.key_to_s, aes.iv_to_s
end

.decrypt(encrypted_source, base64_source = false, key_string = nil, iv_string = nil) ⇒ String

Creates an AES class and then Decrypts source using AES 256 CBC, using @key and @iv

Parameters:

  • encrypted_source (String|File)
  • base64_source (Boolean) (defaults to: false)

    if true, then source is assumed to be base64 encoded.

  • key_string (String) (defaults to: nil)

    optional base64 key to be used in encryption or decryption. if nil, then key and iv are generated automatically. Recover the key with key_to_s(), or key_iv_to_s()

  • iv_string (String) (defaults to: nil)

    optional base64 iv (initial vector) to be used in the encryption or decryption Overwritten by auto generated iv, if key_string is nil. Recover with iv_to_str() or key_iv_to_s().

Returns:

  • (String)

    String representing the original unencypted source



194
195
196
197
# File 'lib/wikk_aes_256.rb', line 194

def self.decrypt(encrypted_source, base64_source = false, key_string = nil, iv_string = nil)
  aes = self.new(key_string, iv_string)
  return aes.decrypt(encrypted_source, base64_source)
end

.encrypt(unencrypted_source, key_string = nil, iv_string = nil) ⇒ String

Encrypts source using AES 256 CBC, using @key and @iv

Parameters:

  • unencrypted_source (String|File)
  • key_string (String) (defaults to: nil)

    optional base64 key to be used in encryption or decryption. if nil, then key and iv are generated automatically. Recover the key with key_to_s(), or key_iv_to_s()

  • iv_string (String) (defaults to: nil)

    optional base64 iv (initial vector) to be used in the encryption or decryption Overwritten by auto generated iv, if key_string is nil. Recover with iv_to_str() or key_iv_to_s().

Returns:

  • (String, String, String)

    Binary string representing encrypted source; base64 key, @key, so later decryption can be done; base64 initial vector, @iv, so later decryption can be done



165
166
167
168
# File 'lib/wikk_aes_256.rb', line 165

def self.encrypt(unencrypted_source, key_string = nil, iv_string = nil)
  aes = self.new(key_string, iv_string)
  return aes.encrypt(unencrypted_source), aes.key_to_s, aes.iv_to_s
end

.gen_iv_to_sString

Generate random AES_256_CBC initialization vector.

Returns:

  • (String)

    Base64 encoded initialization vector @iv



143
144
145
# File 'lib/wikk_aes_256.rb', line 143

def self.gen_iv_to_s
  return [ OpenSSL::Cipher.new(AES_256_CBC).random_iv ].pack('m').chomp
end

.gen_key_iv_to_sString

Generates a new key using Random string in @key, and random AES_256_CBC initialization vector in @iv

Returns:

  • (String, String)

    Base64 encoded string, @key; Base64 encoded initialization vector @iv



151
152
153
# File 'lib/wikk_aes_256.rb', line 151

def self.gen_key_iv_to_s
  return self.gen_key_to_s, self.gen_iv_to_s
end

.gen_key_to_s(key_length: 32) ⇒ String

Generates a random base64 key.

Returns:

  • (String)

    Base64 encoded string, @key



136
137
138
# File 'lib/wikk_aes_256.rb', line 136

def self.gen_key_to_s(key_length: 32)
  SecureRandom.base64(key_length)
end

Instance Method Details

#cipher_to_s(unencrypted_source = nil) ⇒ String

Converts encrypted source String, @cipher_text, into Base64 String

Parameters:

  • unencrypted_source (String|File) (defaults to: nil)

    If present, then this source is encrypted, otherwise assumes already encrypted.

Returns:

  • (String)

    Base64 string representing encrypted source



109
110
111
112
# File 'lib/wikk_aes_256.rb', line 109

def cipher_to_s(unencrypted_source = nil)
  encrypt(unencrypted_source) if unencrypted_source != nil
  return [ @cipher_text ].pack('m').chomp
end

#decrypt(encrypted_source, base64_source = false) ⇒ String

Decrypts source using AES 256 CBC, using @key and @iv

Parameters:

  • encrypted_source (String|File)
  • base64_source (Boolean) (defaults to: false)

    if true, then source is assumed to be base64 encoded.

Returns:

  • (String)

    String representing the original unencypted source



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/wikk_aes_256.rb', line 119

def decrypt(encrypted_source, base64_source = false)
  encrypted_source = StringIO.new(encrypted_source) if encrypted_source.instance_of?(String)
  read_count = base64_source ? 5464 : 4096
  decode_cipher = OpenSSL::Cipher.new(AES_256_CBC)
  decode_cipher.decrypt
  decode_cipher.key = @key
  decode_cipher.iv = @iv
  @plain_text = ''
  while (et = encrypted_source.read(read_count)) != nil
    @plain_text << (base64_source ? decode_cipher.update(et.unpack1('m')) : decode_cipher.update(et))
  end
  @plain_text << decode_cipher.final
end

#encrypt(unencrypted_source) ⇒ String

Encrypts source using AES 256 CBC, using @key and @iv

Parameters:

  • unencrypted_source (String|File)

Returns:

  • (String)

    Binary string representing encrypted source



94
95
96
97
98
99
100
101
102
103
# File 'lib/wikk_aes_256.rb', line 94

def encrypt(unencrypted_source)
  unencrypted_source = StringIO.new(unencrypted_source) if unencrypted_source.instance_of?(String)
  aes = OpenSSL::Cipher.new(AES_256_CBC)
  aes.encrypt
  aes.key = @key
  aes.iv = @iv
  @cipher_text = ''
  while (s = unencrypted_source.read(4096)) != nil do @cipher_text << aes.update(s); end
  @cipher_text << aes.final
end

#gen_ivString

Generate random AES_256_CBC initialization vector.

Returns:

  • (String)

    Binary initialization vector @iv



63
64
65
# File 'lib/wikk_aes_256.rb', line 63

def gen_iv
  return (@iv = OpenSSL::Cipher.new(AES_256_CBC).random_iv)
end

#gen_key(key_length: 32) ⇒ String

Generates a new binary key in @key, using SecureRandom.

Returns:

  • (String)

    Binary string, @key



41
42
43
# File 'lib/wikk_aes_256.rb', line 41

def gen_key(key_length: 32)
  @key = SecureRandom.gen_random(key_length)
end

#iv_to_sString

Convert initialization vector to base64 string

Returns:

  • (String)

    return Base64 version of initialization vector @iv



70
71
72
# File 'lib/wikk_aes_256.rb', line 70

def iv_to_s
  return [ @iv ].pack('m').chomp
end

#key_iv_to_sString

Convert key and the initialization vector into base64 strings

Returns:

  • (String, String)

    base64 version of @key; Base64 version of initialization vector @iv



86
87
88
# File 'lib/wikk_aes_256.rb', line 86

def key_iv_to_s
  return key_to_s, iv_to_s
end

#key_to_sString

Convert key to a base64 string

Returns:

  • (String)

    base64 version of @key



48
49
50
# File 'lib/wikk_aes_256.rb', line 48

def key_to_s
  return [ @key ].pack('m').chomp
end

#str_to_iv(base64_iv_string) ⇒ Array

Convert base64 string into an initialization vector

Parameters:

  • turns (String)

    base64 version of iv into AES_256_CBC initialization vector.

Returns:

  • (Array)

    AES_256_CBC initialization vector @iv.



78
79
80
# File 'lib/wikk_aes_256.rb', line 78

def str_to_iv(base64_iv_string)
  return (@iv = base64_iv_string.unpack1('m'))
end

#str_to_key(base64_keystring) ⇒ String

Convert a base64 string into a key

Parameters:

  • converts (String)

    base64 version of key into AES_256_CBC Symetric Key.

Returns:

  • (String)

    Binary string, @key



56
57
58
# File 'lib/wikk_aes_256.rb', line 56

def str_to_key(base64_keystring)
  return( @key = base64_keystring.unpack1('m') )
end