Class: Rencrypt
- Inherits:
-
Object
- Object
- Rencrypt
- Defined in:
- lib/REncrypt.rb
Instance Attribute Summary collapse
-
#encrypted_data ⇒ Object
Returns the value of attribute encrypted_data.
-
#encrypted_iv ⇒ Object
Returns the value of attribute encrypted_iv.
-
#encrypted_key ⇒ Object
Returns the value of attribute encrypted_key.
-
#plain_data ⇒ Object
Returns the value of attribute plain_data.
Class Method Summary collapse
-
.clear_sensitive ⇒ Object
Holdover from from the conversion from a Model.
-
.decrypt_sensitive(privkey, encrypted_data, encrypted_key, encrypted_iv, password) ⇒ Object
Decrypt the previously encrypted data * privkey is the pathname to the private openssl key.
-
.encrypt_sensitive(pubkey, data) ⇒ Object
Encrypt data using a previously created public key * The fuction will create a random key and random iv * Returns the data, key and IV used to encrypt the data * Data, key and IV should be stored for retrieval later.
Instance Attribute Details
#encrypted_data ⇒ Object
Returns the value of attribute encrypted_data.
15 16 17 |
# File 'lib/REncrypt.rb', line 15 def encrypted_data @encrypted_data end |
#encrypted_iv ⇒ Object
Returns the value of attribute encrypted_iv.
15 16 17 |
# File 'lib/REncrypt.rb', line 15 def encrypted_iv @encrypted_iv end |
#encrypted_key ⇒ Object
Returns the value of attribute encrypted_key.
15 16 17 |
# File 'lib/REncrypt.rb', line 15 def encrypted_key @encrypted_key end |
#plain_data ⇒ Object
Returns the value of attribute plain_data.
15 16 17 |
# File 'lib/REncrypt.rb', line 15 def plain_data @plain_data end |
Class Method Details
.clear_sensitive ⇒ Object
Holdover from from the conversion from a Model. Might not be needed. Yet to be seen.
48 49 50 |
# File 'lib/REncrypt.rb', line 48 def self.clear_sensitive self.encrypted_data = self.encrypted_key = self.encrypted_iv = nil end |
.decrypt_sensitive(privkey, encrypted_data, encrypted_key, encrypted_iv, password) ⇒ Object
Decrypt the previously encrypted data
-
privkey is the pathname to the private openssl key. Make sure its readable by your user
-
encrypted_data is the actual data to be unencrypted
-
encrypted_key is the key used on the previously
-
encrypted_iv is the initialization vector previously used previously
-
password is the private key password used when the OpenSSL private key was created
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/REncrypt.rb', line 24 def self.decrypt_sensitive(privkey, encrypted_data, encrypted_key, encrypted_iv, password) if encrypted_data begin private_key = OpenSSL::PKey::RSA.new(File.read(privkey),password) rescue Exception => e return "There was a problem with the private key: #{e}" end cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc') cipher.decrypt begin cipher.key = private_key.private_decrypt(encrypted_key) cipher.iv = private_key.private_decrypt(encrypted_iv) rescue Exception => e return "There was a problem with the key or IV: #{e}" end decrypted_data = cipher.update(encrypted_data) decrypted_data << cipher.final return decrypted_data else return "Error! No data to decrypt" end end |
.encrypt_sensitive(pubkey, data) ⇒ Object
Encrypt data using a previously created public key
-
The fuction will create a random key and random iv
-
Returns the data, key and IV used to encrypt the data
-
Data, key and IV should be stored for retrieval later
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/REncrypt.rb', line 56 def self.encrypt_sensitive(pubkey, data) if data begin public_key = OpenSSL::PKey::RSA.new(File.read(pubkey)) rescue Exception => e return "There was a problem with the public key: #{e}" end cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc') cipher.encrypt cipher.key = random_key = cipher.random_key cipher.iv = random_iv = cipher.random_iv encrypted_data = cipher.update(data) encrypted_data << cipher.final encrypted_key = public_key.public_encrypt(random_key) encrypted_iv = public_key.public_encrypt(random_iv) return edata = [encrypted_data, encrypted_key, encrypted_iv] else return "No data to encrypt" end end |