0

In rails i have used gem => attr_encrypted using above gem data encrypted in PostgreSQL DB table.

using model name can be decrypt. But how I can do in raw query in select query like i have column encrypted in table like email encrypted and email_encrypted_iv

now I want to use raw query in rails application like

User.select('email, id')

Using above query how we can get data decrypted in select query?

vikasrb
  • 1
  • 2
  • What version of Rails are you using? If you're on Rails 7 it has built in attribute encryption so you do not need that gem. https://guides.rubyonrails.org/active_record_encryption.html – max May 02 '23 at 14:31
  • Rails => 4.2.4 and ruby 2.3.1 , In model i have given secret_key and defined column name as attr_encrypted, so in db table column be like encrypted_email – vikasrb May 03 '23 at 11:16
  • Why bother with encryption when you're on a version of Rails that was EOL eons ago and has known secuity vulnerabities? You should be focused on updating instead. – max May 03 '23 at 11:18
  • Yes, it will updated in soon time, in column previous data stored in plain text, but cause of security added encryption for db table data store, but in some model field data fetched in raw query. – vikasrb May 03 '23 at 11:20
  • 1
    I don't think you can do this without hydrating full `User` objects and letting `attr_encrypted` do its thing. This is the entire purpose of this functionality: to make data at rest (fields in the database) unreadable to the database or whoever steals your data. – Sergio Tulentsev May 04 '23 at 12:28

1 Answers1

0

@Sergio is right, you can't decrypt the database data via SQL when Rails did the encrypting.

You'll have to pull out the record(s) with Rails and do the decryption via the gem.

Chiperific
  • 4,428
  • 3
  • 21
  • 41