1

I used danpal/attr_encryptor gem to encrypt some attribute in my ActiveRecord model.

attr_encrypted :number, :charset => 'UTF-8', :key => 'vasya pupkin'

and got following error

OpenSSL::Cipher::CipherError: bad decrypt

Then I tried to use encrypt_number in rails console and seen different values for the same value of 'number' field when I restarted console. Why?

rails c
CreditCard.encrypt_number('hello')
"tRO0BETHrh5J3gXv8WVntw==\n"
quit
rails c
CreditCard.encrypt_number('hello')
"kJSbe30RQyR+gy3oDXv5ZA==\n"
maxs
  • 427
  • 1
  • 6
  • 15

1 Answers1

0

In the case of the bad decrypt error, have you created encrypted_number_iv and encrypted_number_salt columns in your database and is the gem populating them for you automatically?

In the case of encrypt_number, you need to pass :iv and :salt options (the same each time) to receive the same ciphertext at the output.

Richard Cook
  • 32,523
  • 5
  • 46
  • 71