-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ArgumentError: key must be 32 bytes #26
Comments
This is affecting my Rails 5.0.1 application too, following an upgrade to Ruby 2.4 |
Same issue for me when upgrading to Ruby 2.4.0. |
I don't think that the issue really comes from We've solved it by generating keys that are the correct size length; If you plan to store the keys in a YML file (or similar), you will probably have to resort to using Base64. We did this: namespace :openssl do
desc "Generate Base64 encoded bytes for application.yml"
task generate: :environment do
{
ENCRYPT_SECRET_KEY: 32,
ENCRYPT_IV: 12,
ENCRYPT_SALT: 128
}.each do |k, length|
value = Base64.encode64(SecureRandom.random_bytes(length)).delete("\n")
puts "#{k}: '#{value}'"
end
end
end Then in # frozen_string_literal: true
Encryptor.default_options.merge!(
algorithm: 'aes-256-gcm',
key: Base64.decode64(ENV['ENCRYPT_SECRET_KEY']),
iv: Base64.decode64(ENV['ENCRYPT_IV']),
salt: Base64.decode64(ENV['ENCRYPT_SALT'])
) |
Hi there! Could you tell me how I can migrate from 128 byte key to 32 byte key in production? |
@anaumov We were able to simply truncate our keys from their longer length to 32 bytes. It seems that while the Ruby SSL implementation allowed for keys longer than 32 bytes, it truncated them. The recent change is to raise an error for keys that aren't exactly 32 bytes. Obviously, you should test this to make sure it's works for you :). |
A simple key truncation also worked in our case. |
Hi there, I got a error like almost same thing, but iv not key. Hi there, I also encountered the same kind of error but IV not key. |
This is a really frustrating issue. It would be great if someone could post their solution to this problem. I've tried the Base64 encode/decode method to no avail. I also arrived here because I'm using the |
@danielricecodes people have posted their solution. Use the correct sized key and iv. You can trim the extra bytes and it should work. However, I'm not certain how the IV was handled in Ruby < 2.4. If it was similarly ignored like the extra bytes in the key, then you should be able to simply trim the IV. |
Here's a way I figured out can generate a 32 character key without using SecureRandom... Just run this in `rake secret`[0..31] It will spit out a 32 byte string - just without all the random garbage SecureRandom will throw in there. You don't have to Base64 encode that value either. |
@danielricecodes I don't recommend doing that, but you're welcome to do whatever you like. |
I downgraded my app to Ruby 2.3 as a workaround. IMHO, it appears the So perhaps an Issue needs to be opened for Ruby 2.4 support for PS, I was testing with Ruby 2.4, Rails 5.0.2, and UPDATE (Sep 26th, 2017): This gem seems to work now with Rails 5.0.6 and Ruby 2.4.2. |
@danielricecodes correct, there isn't official support for Ruby 2.4 yet. People are using it at their own risk. I've started work to support it but I've hit a few issues. |
I think it'd be a big help to improve this line to warn people about this issue. raise ArgumentError.new("key must be #{cipher.key_len} bytes or longer") if options[:key].bytesize < cipher.key_len The "or longer" is not true in this case. |
I'm still running into this issue with Ruby 2.3.4 and Rails 5.0.1. Moreover, my key length indicates that it is 32 bytes, so I don't understand what is happening. It's very frustrating. UDPATE/EDIT: Updating in case anyone else should stumble in here searching. My |
how do you migrate from a shorter key? (21 bytes) |
@NullVoxPopuli ...
Or, at least that's what I would do. This is a useful procedure to try and do, since it's also how you deal with a possibly exposed secret. |
cool, thanks for the tip! |
Not sure if this the "correct" answer, but @consti's suggestion worked for me. |
attr-encrypted/encryptor#26 ***Temporary*** * This is a temporary fix to make this work and get all the CI tests working. Not the most secure solution
My key was 32 chars, but 41 bytes because the key was stored in UTF-8. I was able to convert it, truncate to 32 bytes, then store in yml using base64 as suggested by @consti . I converted it something like: |
Are we planning to change key_len & iv_len checking logic? Code needs to check exact lengths. Happy to raise a PR if its correct approach. |
I was able to upgrade to Rails 2.5.5, without changing my old keys, using @pduey approach
|
@pduey or someone, would you mind explaining the |
Array#pack converts an integer array to a string. The parameter is a format string. In our case, it's interpreting the first 32 elements of the array as a signed integer and printing it in string form. "c" * 32 is shorthand for "cccccccccccccccccccccccccccccccc". Array#pack also accepts "c*" which says interpret the entire array, which would be safe in our case since we also used bytes[0..31]. Also, String#unpack, the converse of Array#pack, could be used instead of String#bytes. |
a simple |
I'm using
encryptor
viaattr-encrypted
in a Rails 5 application. While evaluating an upgrade to Ruby 2.4.0, I came across the following error coming out of this gem. Not sure if this is a bug, or something I'm doing wrong, or some change related to Ruby 2.4.0 that requires some effort on my part.From Ruby 2.4.0
Previous behavior from Ruby 2.3.3
These are both using encryptor 3.0.0.
The text was updated successfully, but these errors were encountered: