Class: WIKK::AES_256
- Inherits:
-
Object
- Object
- WIKK::AES_256
- 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
-
#cipher_text ⇒ String
readonly
the encrypted text.
-
#plain_text ⇒ String
readonly
the decrypted text.
Class Method Summary collapse
-
.cipher_to_s(unencrypted_source, key_string = nil, iv_string = nil) ⇒ String
Converts encrypted source String, @cipher_text, into Base64 String.
-
.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.
-
.encrypt(unencrypted_source, key_string = nil, iv_string = nil) ⇒ String
Encrypts source using AES 256 CBC, using @key and @iv.
-
.gen_iv_to_s ⇒ String
Generate random AES_256_CBC initialization vector.
-
.gen_key_iv_to_s ⇒ String
Generates a new key using Random string in @key, and random AES_256_CBC initialization vector in @iv.
-
.gen_key_to_s(key_length: 32) ⇒ String
Generates a random base64 key.
Instance Method Summary collapse
-
#cipher_to_s(unencrypted_source = nil) ⇒ String
Converts encrypted source String, @cipher_text, into Base64 String.
-
#decrypt(encrypted_source, base64_source = false) ⇒ String
Decrypts source using AES 256 CBC, using @key and @iv.
-
#encrypt(unencrypted_source) ⇒ String
Encrypts source using AES 256 CBC, using @key and @iv.
-
#gen_iv ⇒ String
Generate random AES_256_CBC initialization vector.
-
#gen_key(key_length: 32) ⇒ String
Generates a new binary key in @key, using SecureRandom.
-
#initialize(key_string = nil, iv_string = nil) ⇒ AES_256
constructor
Initialize.
-
#iv_to_s ⇒ String
Convert initialization vector to base64 string.
-
#key_iv_to_s ⇒ String
Convert key and the initialization vector into base64 strings.
-
#key_to_s ⇒ String
Convert key to a base64 string.
-
#str_to_iv(base64_iv_string) ⇒ Array
Convert base64 string into an initialization vector.
-
#str_to_key(base64_keystring) ⇒ String
Convert a base64 string into a key.
Constructor Details
#initialize(key_string = nil, iv_string = nil) ⇒ AES_256
Initialize
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_text ⇒ String (readonly)
the encrypted text
12 13 14 |
# File 'lib/wikk_aes_256.rb', line 12 def cipher_text @cipher_text end |
#plain_text ⇒ String (readonly)
the decrypted 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
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
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
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_s ⇒ String
Generate random AES_256_CBC initialization vector.
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_s ⇒ String
Generates a new key using Random string in @key, and random AES_256_CBC initialization vector in @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.
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
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
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
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_iv ⇒ String
Generate random AES_256_CBC initialization vector.
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.
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_s ⇒ String
Convert initialization vector to base64 string
70 71 72 |
# File 'lib/wikk_aes_256.rb', line 70 def iv_to_s return [ @iv ].pack('m').chomp end |
#key_iv_to_s ⇒ String
Convert key and the initialization vector into base64 strings
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_s ⇒ String
Convert key to a base64 string
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
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
56 57 58 |
# File 'lib/wikk_aes_256.rb', line 56 def str_to_key(base64_keystring) return( @key = base64_keystring.unpack1('m') ) end |