-
Notifications
You must be signed in to change notification settings - Fork 92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#encrypted? returns true for a plain text string #63
Comments
Closing this issue as regenerating the keys fixed it. Wondering if maybe the recent openssl updates affected this (wouldn't be surprised). |
This happened again today with a different string, so I'm not going to rely upon this method for now.
this is in development and my config/symmetric-encryption.rb file looks like:
|
Correct, using this method with random data will result in false positives. The method attempts to decrypt the data and since it is valid base64 it will decode, and then it attempts to decrypt it with OpenSSL. In a small amount of cases it will decrypt without OpenSSL raising an exception. Since you have In this case where all encryption keys have I have the following method locally that I started but never committed to extract the header: module SymmetricEncryption
# Returns the header for the encrypted string
# Returns [nil] if no header is present
def self.header(encrypted_and_encoded_string)
raise(SymmetricEncryption::ConfigError, 'Call SymmetricEncryption.load! or SymmetricEncryption.cipher= prior to encrypting or decrypting data') unless @@cipher
return if encrypted_and_encoded_string.nil? || (encrypted_and_encoded_string == '')
# Decode before decrypting supplied string
decoded = cipher.encoder.decode(encrypted_and_encoded_string.to_s)
return if decoded.nil? || decoded.empty?
Cipher.parse_header!(decoded)
end
end If you want to give a try and give me some feedback I will be happy to publish it in a new gem. The part that is not finished is what should be returned from this method. |
It works fine if I adjust the "decoded" line to Base64.decode64(encrypted_and_encoded_string.to_s). Otherwise, I get the following error:
No need for a new gem, I'll just use this way of doing it to check if a string is encrypted. Thanks. |
For some reason, using the 'encrypted?' method against the following string returns true when it shouldn't. I've tried with numerous other strings and can't get the above behavior. Also, with the same exact key and iv, I can encrypt and decrypt just fine:
`irb(main):001:0> require_relative("boot")
=> true
irb(main):002:0> str = "LIDIA MARGARITA MARTINEZ"
=> "LIDIA MARGARITA MARTINEZ"
irb(main):003:0> enc = SymmetricEncryption.encrypt(str)
=> "UPWRrAPEY+ngnsdZP0C3M6tJlwfnf6Lu3V7DDXjXLm4="
irb(main):005:0> dec = SymmetricEncryption.decrypt(enc)
=> "LIDIA MARGARITA MARTINEZ"
irb(main):006:0> dec == str
=> true
Here is where the issue occurs:
irb(main):007:0> SymmetricEncryption.encrypted?(str)
=> true
`
The text was updated successfully, but these errors were encountered: