shashank barman
shashank barman

Reputation: 31

How do I decrypt in rails?

Hello fellow developers,

I have been stuck on this isssue for quite sometime now. All I want to do is somehow decrypt a column (ssn_or_ein) in my table (candidates) which was previously encrypted by the gem crypty_keeper. https://github.com/jmazzi/crypt_keeper

What i have tried so far: required the gem in Rails C and used the decrypt_table! against my Model

However, I am unable to catch the method

[pry(main)> Candidate.decrypt_table!
   (0.2ms)  BEGIN
   Load (12.1ms)  SELECT  "candidates".* FROM "candidates" ORDER BY "candidates"."id" ASC LIMIT $1  [["LIMIT", 1000]]
   (4.8ms)  ROLLBACK
NameError: undefined local variable or method `crypt_keeper_fields' for #<Class:0x0000000007c5f038>
Did you mean?  crypt_keeper

Also tried putting the pre-existing code back in my Application

class Candidate < ApplicationRecord


   crypt_keeper :gateway_token, :ssn_or_ein, :e_in, encryptor: :active_support, key: ENV['ENCRYPTION_KEY'], salt: ENV['ENCRYPTION_SALT']

and I get an Active Support error

pry(main)> Candidate.decrypt_table!
   (0.3ms)  BEGIN
   Load (609.8ms)  SELECT  "candidates".* FROM "candidates" ORDER BY "candidates"."id" ASC LIMIT $1  [["LIMIT", 1000]]
   (0.3ms)  ROLLBACK
ActiveSupport::MessageEncryptor::InvalidMessage: ActiveSupport::MessageEncryptor::InvalidMessage
from /home/niketa/.rvm/gems/[email protected]/gems/activesupport-5.2.6/lib/active_support/message_encryptor.rb:206:in `rescue in _decrypt'
Caused by ArgumentError: invalid base64

This is the error i am getting.

Any help or insight would be appreciated. Thanks

Upvotes: 0

Views: 238

Answers (1)

eikes
eikes

Reputation: 5061

It looks like you have rows in your table that are not encrypted and those cause the Candidate.decrypt_table! call to fail. Did you maybe remove the crypt_keeper line from the Candidate class and then add entries? If so you'll have to remove them or skip them...

Upvotes: 1

Related Questions