attr-encrypted / attr_encrypted

Generates attr_accessors that encrypt and decrypt attributes
MIT License
2.01k stars 427 forks source link

Cannot find by encrypted field #130

Open josegrad opened 10 years ago

josegrad commented 10 years ago

Hi,

I'm using version 1.3.3 with Rails 4.0.0 and Ruby 1.9.3. I can store encrypted fields but using a find or where with mongoid 4.0.0.beta1 returns nothing.

class Discount
    include Mongoid::Document
    include Mongoid::Timestamps

    field :encrypted_email, :type => String
    attr_encrypted :email, :key => ENV['DISK'], :encode => true, :charset => "utf-8"

    field :encrypted_address, :type => String
    attr_encrypted :address, :key => ENV['DISK'], :encode => true, :charset => "utf-8"

end

Discount.first.email
 => "lucas@email.com"

Discount.where(:email => "lucas@email.com").first
 => nil 

Any hints?

ricochet2200 commented 10 years ago

I am having a similar problem, but with the order() function. I can order using the encrypted column, but that is hardly helpful ;) Perhaps I'm doing this wrong?

ivandenysov commented 10 years ago

Did you try searching by encrypted value? https://github.com/attr-encrypted/attr_encrypted#encryptdecrypt-attribute-methods

Discount.where(:encrypted_email => Discount.encrypt_email("lucas@email.com"))
billymonk commented 10 years ago

Hi All,

@john-denisov: Thanks for posting what I've been meaning to post the past couple of weeks.

@josegrad: Please see @john-denisov's comment.

@ricochet2200: This gem is unfortunately not going to help you. An easy (but unfortunately not ideal) solution would be to handle the ordering in Ruby.

josegrad commented 10 years ago

:-) That looks funny, clever and a bit ugly. But I can buy it if it works. I'll check it out and report back. Looks like it should work.

Thanks.

ricochet2200 commented 10 years ago

@billymonk Gotcha. I figured I might have to do that, but wanted to make sure that was the "blessed" way to do it with this gem. Thanks for responding.

spq24 commented 9 years ago

Hi @billymonk

I'm having an issue with this. I'm trying to use the find_by methods in the documentation and although they are set up the right way it doesn't seem to be working.

I'm using this to encrypt stripe tokens for my customers.

A given stripe customers unencrypted token is: cus_6ZqTwcXuvymA7SFD

When I do c = customer.find(158) and get the customer back and look at c.stripe_token it comes back correctly and it's encrypted in the db so I know it's working.

However when I do Customer.find_by_stripe_token('cus_6ZqTwcXuvymA7SFD')

I get nil back. When I look at the encrypted stripe token in the SQL query it does not match the encrypted version for this customer's token.

I have done this all in the console so far so I don't really have code to share, but I can put it together real quick if you think that would help, but if it works in code it should work in the console.

Also, the key hasn't changed since this customer was created.

Any ideas?

billymonk commented 9 years ago

HI @spq24,

Without more details it would be hard to say for sure what the issue is. Have you changed your key since you encrypted the data?

If you want to provide some more information we can definitely get to the bottom of this.

spq24 commented 9 years ago

really weird...restarted the server and it worked. Guess it was an anomaly I have no idea what happened there. Thanks anyway!

billymonk commented 9 years ago

@spq24,

I'm glad it sorted itself out!

billymonk commented 9 years ago

@josegrad,

Did you have the opportunity to try @john-denisov proposed? If it worked or you found another work around I'll close the issue.

CoralineAda commented 9 years ago

Having the same problem:

In user.rb:

  attr_encrypted  :email,
                  :key => ENV['EMAIL_KEY'],
                  :attribute => 'encrypted_email'

In rails c:

u = User.create(email: "hello@example.com")
INSERT INTO "users" ("encrypted_email", "created_at", "updated_at") VALUES (?, ?, ?)  [["encrypted_email", "$2a$10$i2U2BueGjNH2flxeWFrIoeVAKkCZlAN0Ak.IRjgyPV7UyX1RTHqi2"], ["created_at", "2015-10-14 00:48:24.063898"], ["updated_at", "2015-10-14 00:48:24.063898"]]

User.find_by_email("hello@example.com")
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."encrypted_email" = ? LIMIT 1  [["encrypted_email", "$2a$10$BKw0tiuXTLLN3xYWkiz9me9mVDjZO2Z9bGbbFplRx2kVHKflWgvAy"]]
 => nil 

Note that the encrypted email value is different in the create than it is in the find.

spq24 commented 9 years ago

did you change the encryption key you use at all? Also, for me restarting the server randomly made it work somehow.

sscirrus commented 7 years ago

@john-denisov This is the closest I've come, but it's still giving me this:

User.where(:e_key => User.encrypt_key("b17...d2f")) ArgumentError: must specify an iv

This doesn't work because I've got a User.e_key_ivfield, which means the salt is different for every record.

I've tried many ways of doing a search for a record by key but I can't seem to do it at all.

HLFH commented 7 years ago

@sscirrus Symmetric Encryption works well with Mongoid, FYI.