Encrypting with attr-encrypt gem but can't decrypt

768 Views Asked by At

Using gem: https://github.com/attr-encrypted/attr_encrypted

Issue 1:

When I create the record and encrypt, I go to the console (via Heroku) and do:

model = Model.find(1)
model.attribute

Error: OpenSSL::Cipher::CipherError ()

Issue 2:

I create the record, go to edit, the params show decrypted (not sure how if it won't work in console, but does)...

Once I recommit (git push heroku master) or restart heroku, the edit page gives me this error: ActionView::Template::Error ()

The error is at the line for the text_field attribute publishable key

My setup:

Schema:

    t.string "encrypted_publishable_key"
    t.string "encrypted_publishable_key_iv"
    t.string "encrypted_secret_key"
    t.string "encrypted_secret_key_iv"
    t.index ["encrypted_publishable_key_iv"], name: "index_stripe_apis_on_encrypted_publishable_key_iv", unique: true
    t.index ["encrypted_secret_key_iv"], name: "index_stripe_apis_on_encrypted_secret_key_iv", unique: true

Tried removing the index's but still errors persist.

Model:

key = Base64.encode64(SecureRandom.random_bytes(32))
attr_encrypted :publishable_key, key: Base64.decode64(key)
attr_encrypted :secret_key, key: Base64.decode64(key)

Also tried with:

key = Base64.encode64(SecureRandom.random_bytes(32))
iv = Base64.encode64(SecureRandom.random_bytes(12))
attr_encrypted :publishable_key, key: Base64.decode64(key), iv: Base64.decode64(iv)
attr_encrypted :secret_key, key: Base64.decode64(key), iv: Base64.decode64(iv)

Wihtout key = Base64.encode64(SecureRandom.random_bytes(32)) I get the error "32 bytes required".

With the above, the attributes will save but on decryption, both in views and console, I get error:

iv must be 12 bytes or longer

What am I doing wrong that this will not work?

0

There are 0 best solutions below