Alex Rivera | Logout

rails encryption/decryption

Asked 2011-12-19T03:55:59.907
9

i need to do encryption and decryption in my rails app. Im trying to use ezcrypto, but whenever i do decryption i get this error.

OpenSSL::Cipher::CipherError in ProfilesController#show

wrong final block length

What would need to be changed to stop this error. I tried using another implementation of openssl like this (methods to be called from my model)

def encrypt_attr(unencrypted)
    c = OpenSSL::Cipher.new("aes-256-cbc")
    c.encrypt
    c.key = Digest::SHA1.hexdigest('pass')
    e = c.update(unencrypted)
    e << c.final
    return e
end

def decrypt_attr(encrypted_attr)
  if encrypted_attr != ""
    c = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
    c.decrypt
    c.key = Digest::SHA1.hexdigest('pass')
    d = c.update(encrypted_attr)
    d << c.final
    return d
  end
end

It throws the exact same error on decryption. How should i do encryption and decryption and not get this openssl error.

Edit
Report

1 Answer

0

I had the same problem as you and this is how I solved it:

  1. Your database column has to be: t.column :data, :binary, :limit => 10.megabyte.
  2. Use the exact code as this blog post.
  3. Choose AES-256-ECB
  4. Leave ivs as nil
  5. Choose a long key
answered 2013-03-05T15:22:27.233

Your Answer