Class: CryptoCipher::CryptoAES256
- Inherits:
-
Object
- Object
- CryptoCipher::CryptoAES256
- Defined in:
- lib/cipher/crypto_cipher_aes.rb
Instance Attribute Summary collapse
-
#cipher ⇒ Object
Returns the value of attribute cipher.
-
#private_cipher ⇒ Object
Returns the value of attribute private_cipher.
-
#private_key ⇒ Object
Returns the value of attribute private_key.
-
#public_key ⇒ Object
Returns the value of attribute public_key.
-
#random_iv ⇒ Object
Returns the value of attribute random_iv.
-
#random_key ⇒ Object
Returns the value of attribute random_key.
Instance Method Summary collapse
- #decrypt(encrypted_data) ⇒ Object
- #encrypt(plain_data) ⇒ Object
-
#initialize(public_key_file, private_key_file, private_cipher_file = './config/private_cipher.key') ⇒ CryptoAES256
constructor
A new instance of CryptoAES256.
Constructor Details
#initialize(public_key_file, private_key_file, private_cipher_file = './config/private_cipher.key') ⇒ CryptoAES256
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/cipher/crypto_cipher_aes.rb', line 15 def initialize(public_key_file, private_key_file, private_cipher_file = './config/private_cipher.key') @cipher = OpenSSL::Cipher.new('aes256') @private_cipher = private_cipher_file @public_key = OpenSSL::PKey::RSA.new(File.read(public_key_file)) @private_key = OpenSSL::PKey::RSA.new(File.read(private_key_file)) if exist_rsa_keys puts 'create values' @cipher.encrypt @random_key = @cipher.random_key @random_iv = @cipher.random_iv create_key_iv @random_key, @random_iv else rsa = get_key_iv @random_key = rsa[:random_key] @random_iv = rsa[:random_iv] end end |
Instance Attribute Details
#cipher ⇒ Object
Returns the value of attribute cipher.
7 8 9 |
# File 'lib/cipher/crypto_cipher_aes.rb', line 7 def cipher @cipher end |
#private_cipher ⇒ Object
Returns the value of attribute private_cipher.
11 12 13 |
# File 'lib/cipher/crypto_cipher_aes.rb', line 11 def private_cipher @private_cipher end |
#private_key ⇒ Object
Returns the value of attribute private_key.
13 14 15 |
# File 'lib/cipher/crypto_cipher_aes.rb', line 13 def private_key @private_key end |
#public_key ⇒ Object
Returns the value of attribute public_key.
12 13 14 |
# File 'lib/cipher/crypto_cipher_aes.rb', line 12 def public_key @public_key end |
#random_iv ⇒ Object
Returns the value of attribute random_iv.
9 10 11 |
# File 'lib/cipher/crypto_cipher_aes.rb', line 9 def random_iv @random_iv end |
#random_key ⇒ Object
Returns the value of attribute random_key.
8 9 10 |
# File 'lib/cipher/crypto_cipher_aes.rb', line 8 def random_key @random_key end |
Instance Method Details
#decrypt(encrypted_data) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/cipher/crypto_cipher_aes.rb', line 100 def decrypt(encrypted_data) @cipher.decrypt encrypted_key = @public_key.public_encrypt(@random_key) encrypted_iv = @public_key.public_encrypt(@random_iv) @cipher.key = @private_key.private_decrypt(encrypted_key) @cipher.iv = @private_key.private_decrypt(encrypted_iv) decrypted_data = @cipher.update(Base64.decode64(encrypted_data)) + cipher.final return decrypted_data end |
#encrypt(plain_data) ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'lib/cipher/crypto_cipher_aes.rb', line 89 def encrypt(plain_data) @cipher.encrypt @cipher.key = @random_key @cipher.iv = @random_iv encrypted_data = @cipher.update(plain_data) + cipher.final return Base64.strict_encode64(encrypted_data) end |