Class: EncryptedStrings::SymmetricCipher
- Defined in:
- lib/encrypted_strings/symmetric_cipher.rb
Overview
Symmetric encryption uses a specific algorithm and password to encrypt the string. As long as the algorithm and password are known, the string can be decrypted.
Source: support.microsoft.com/kb/246071
Encrypting
To encrypt a string using a symmetric cipher, the algorithm and password must be specified. You can define the defaults for these values like so:
EncryptedStrings::SymmetricCipher.default_algorithm = 'des-ecb'
EncryptedStrings::SymmetricCipher.default_password = 'secret'
If these configuration options are not passed in to #encrypt, then the default values will be used. You can override the default values like so:
password = 'shhhh'
password.encrypt(:symmetric, :algorithm => 'des-ecb', :password => 'secret') # => "S/sEkViX3v4=\n"
An exception will be raised if no password is specified.
Decrypting
To decrypt a string using an symmetric cipher, the algorithm and password must be specified. Defaults for these values can be defined as show above.
If these configuration options are not passed in to #decrypt, then the default values will be used. You can override the default values like so:
password = "S/sEkViX3v4=\n"
password.decrypt(:symmetric, :algorithm => 'des-ecb', :password => 'secret') # => "shhhh"
An exception will be raised if no password is specified.
Class Attribute Summary collapse
-
.default_algorithm ⇒ Object
The default algorithm to use for encryption.
-
.default_password ⇒ Object
The default password to use for generating the key and initialization vector.
Instance Attribute Summary collapse
-
#algorithm ⇒ Object
The algorithm to use for encryption/decryption.
-
#password ⇒ Object
The password that generates the key/initialization vector for the algorithm.
Instance Method Summary collapse
-
#decrypt(data) ⇒ Object
Decrypts the current string using the current key and algorithm specified.
-
#encrypt(data) ⇒ Object
Encrypts the current string using the current key and algorithm specified.
-
#initialize(options = {}) ⇒ SymmetricCipher
constructor
Creates a new cipher that uses a symmetric encryption strategy.
Methods inherited from Cipher
Constructor Details
#initialize(options = {}) ⇒ SymmetricCipher
Creates a new cipher that uses a symmetric encryption strategy.
Configuration options:
-
:algorithm
- The algorithm to use for generating the encrypted string -
:password
- The secret value to use for generating the key/initialization vector for the algorithm
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/encrypted_strings/symmetric_cipher.rb', line 66 def initialize( = {}) = .keys - [:algorithm, :password] raise ArgumentError, "Unknown key(s): #{.join(", ")}" unless .empty? = { :algorithm => SymmetricCipher.default_algorithm, :password => SymmetricCipher.default_password }.merge() self.algorithm = [:algorithm] self.password = [:password] raise NoPasswordError if password.nil? super() end |
Class Attribute Details
.default_algorithm ⇒ Object
The default algorithm to use for encryption. Default is DES-EDE3-CBC.
43 44 45 |
# File 'lib/encrypted_strings/symmetric_cipher.rb', line 43 def default_algorithm @default_algorithm end |
.default_password ⇒ Object
The default password to use for generating the key and initialization vector. Default is nil.
47 48 49 |
# File 'lib/encrypted_strings/symmetric_cipher.rb', line 47 def default_password @default_password end |
Instance Attribute Details
#algorithm ⇒ Object
The algorithm to use for encryption/decryption
54 55 56 |
# File 'lib/encrypted_strings/symmetric_cipher.rb', line 54 def algorithm @algorithm end |
#password ⇒ Object
The password that generates the key/initialization vector for the algorithm
58 59 60 |
# File 'lib/encrypted_strings/symmetric_cipher.rb', line 58 def password @password end |
Instance Method Details
#decrypt(data) ⇒ Object
Decrypts the current string using the current key and algorithm specified
83 84 85 86 |
# File 'lib/encrypted_strings/symmetric_cipher.rb', line 83 def decrypt(data) cipher = build_cipher(:decrypt) cipher.update(data.unpack('m')[0]) + cipher.final end |
#encrypt(data) ⇒ Object
Encrypts the current string using the current key and algorithm specified
89 90 91 92 |
# File 'lib/encrypted_strings/symmetric_cipher.rb', line 89 def encrypt(data) cipher = build_cipher(:encrypt) [cipher.update(data) + cipher.final].pack('m') end |