Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 163 Vote(s) - 3.55 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rails 5 cipher.key "key must be 32 bytes" error

#1
Brand new Rails application.

Rails version 5.0.0.1, Ruby version 2.4.0preview2.

Create application "demo", run a simple scaffold generate Product, and get an error when trying to view the scaffold's overview page (base index file still loads the Welcome to Rails screen fine):

ArgumentError in ProductsController#index
key must be 32 bytes:

cipher = new_cipher
cipher.encrypt
cipher.key = @secret

# Rely on OpenSSL for the initialization vector
iv = cipher.random_iv

The problem line is apparently cipher.key = @secret.

I've seen various mentions on the github repo for Rails mentioning this issue, but all implied it was now resolved in Rails 5.0.0.1
Reply

#2
This issue turns out to be connected to the key you are using. Without changing your key you can use the code below to transform your key to 32 bytes:

> attr_encrypted :attribute, key: ENV['MY_KEY'].bytes[0..31].pack( "c" * 32 )
Reply

#3
Please use **Digest::MD5** to achive 32 bytes

require 'openssl'
require 'digest'
require 'base64'

data = "encrypt me"
secret_key = "asd3dssdf34HDas"
c = OpenSSL::Cipher.new("aes-256-cbc")
c.encrypt
c.key = Digest::MD5.hexdigest(secret_key) # this will convert key length into 32
encrypted_data = c.update(data.to_s) + c.final
encrypted_data = Base64.urlsafe_encode64(encrypted_data, padding: false) #padding: false will remove '/', '+' from encrypted data
encrypted_data.gsub! "\n",""


Or Simply use **secret key** of length 32 bytes

data = "encrypt me"
secret_key = "Aswertyuioasdfghjkqwertyuiqwerty"
c = OpenSSL::Cipher.new("aes-256-cbc")
c.encrypt
c.key = secret_key
encrypted_data = c.update(data.to_s) + c.final

Reply

#4
Had the same error:
Running bundle update should do the trick
Reply

#5
I was having this problem too and fixed it by running

bundle update

Make sure that you have the latest version of rails installed.
Reply

#6
**Solution**:

1. Edit your Gemfile
2. Add the following line: gem 'rails', '~> 5.0.0', '>= 5.0.0.1'
3. bundle install
4. Optional: I am using ruby2-4.1 . (rvm install ruby-2.4.1)

**Rational**: The rails version prior to 5.0.0 seems to have a bug that causes this issue. The bug has been resolved in the latest version of Rails. If you hare following the Rails Installation Guide (

[To see links please register here]

) you will probably encounter this problem as of this posting date.

This fix does work, and is verified by
Reply

#7
Finally found problem! It was from a bugfix...

[To see links please register here]


If you are using cipher e.g. 'aes-256-cfb', the `key_len` is 32, found by:

require 'openssl'
cipher = OpenSSL::Cipher.new('aes-256-cfb')
cipher.key_len # => 32

We had mistakenly thought we needed to send a 256 character nonce, but actually you are supposed to send a 32 character nonce - or
use `cipher.random_key` (which internally uses the `key_len`). It never used to be a problem because openssl truncated the nonce... but now you need to send the right lengthed nonce.


We got this error upgrading ruby from 2.3.4 to 2.4.2.
Reply

#8
Use `random_key` so it always fit.

key = cipher.random_key
cipher.key = key

reference

[To see links please register here]

Reply

#9
try this:

rake db:create
rake db:migrate

then, the most important thing:

bundle update
This works for me.
Reply

#10
Ok, there was a slight misunderstanding on my part, looks like the fix is coming in 5.0.1 not 5.0.0.1

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through