Skip to content
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: must specify an iv #1

Open
jbrown296 opened this issue Jan 8, 2017 · 7 comments
Open

ArgumentError: must specify an iv #1

jbrown296 opened this issue Jan 8, 2017 · 7 comments

Comments

@jbrown296
Copy link

I'm having an unexpected error and am pretty stumped here. I'm pretty new to Rails so maybe I am missing something simple.
I want to revert the encrypted field of date_of_birth on my Users model so I followed the ReadMe.

I'm using:
attr_encrypted (3.0.3)
encryptor (3.0.0)
encryption_migrator (0.1.0)

After I installed the gem, I commented out the attr_encrypted attribute:
# attr_encrypted :date_of_birth, key: ENV["USER_DOB"]

I then ran the following migration:

class UnencyptFields < ActiveRecord::Migration
  def up
  	unencrypt_field :users, :date_of_birth, key: ENV["USER_DOB"]
  end
end

So far, so good.

However, when I run rake db:migrate I get an error that I "must specify an iv." I am dropping the trace below. Any help is appreciated. Thanks!!

== 20170108150730 UnencyptFields: migrating ===================================
-- add_column(:users, :date_of_birth, :string)
   -> 0.0620s
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

must specify an iv/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/encryptor-3.0.0/lib/encryptor.rb:61:in `crypt'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/encryptor-3.0.0/lib/encryptor.rb:49:in `decrypt'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/attr_encrypted-3.0.3/lib/attr_encrypted.rb:233:in `decrypt'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/encryption_migrator-0.1.0/lib/encryption_migrator.rb:17:in `decrypt_and_update_row'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/encryption_migrator-0.1.0/lib/encryption_migrator.rb:35:in `block in unencrypt_field'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/relation/delegation.rb:46:in `each'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/relation/delegation.rb:46:in `each'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/encryption_migrator-0.1.0/lib/encryption_migrator.rb:34:in `unencrypt_field'
/Users/jbrown/Desktop/Rails Workspace/PMS/db/migrate/20170108150730_unencypt_fields.rb:3:in `up'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:611:in `exec_migration'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:592:in `block (2 levels) in migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:591:in `block in migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:590:in `migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:768:in `migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:998:in `block in execute_migration_in_transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:1044:in `block in ddl_transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/transactions.rb:220:in `transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:1044:in `ddl_transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:997:in `execute_migration_in_transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:959:in `block in migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:955:in `each'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:955:in `migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:823:in `up'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:801:in `migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
ArgumentError: must specify an iv
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/encryptor-3.0.0/lib/encryptor.rb:61:in `crypt'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/encryptor-3.0.0/lib/encryptor.rb:49:in `decrypt'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/attr_encrypted-3.0.3/lib/attr_encrypted.rb:233:in `decrypt'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/encryption_migrator-0.1.0/lib/encryption_migrator.rb:17:in `decrypt_and_update_row'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/encryption_migrator-0.1.0/lib/encryption_migrator.rb:35:in `block in unencrypt_field'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/relation/delegation.rb:46:in `each'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/relation/delegation.rb:46:in `each'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/encryption_migrator-0.1.0/lib/encryption_migrator.rb:34:in `unencrypt_field'
/Users/jbrown/Desktop/Rails Workspace/PMS/db/migrate/20170108150730_unencypt_fields.rb:3:in `up'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:611:in `exec_migration'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:592:in `block (2 levels) in migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:591:in `block in migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:590:in `migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:768:in `migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:998:in `block in execute_migration_in_transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:1044:in `block in ddl_transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/transactions.rb:220:in `transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:1044:in `ddl_transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:997:in `execute_migration_in_transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:959:in `block in migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:955:in `each'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:955:in `migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:823:in `up'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:801:in `migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate
@tomkadwill
Copy link
Owner

Hey @jbrown296, I think you might be missing an option: https://github.com/attr-encrypted/encryptor/blob/master/lib/encryptor.rb#L45-L47

In your migration you've written this line of code: unencrypt_field :users, :date_of_birth, key: ENV["USER_DOB"] but you're missing the iv option.

The example project in the REAME didn't require that option but it looks like your project does. It should just be a case of adding the iv option to your unencrypt_field method call, in the migration

@jbrown296
Copy link
Author

jbrown296 commented Jan 14, 2017

Hi @tomkadwill,

I added the iv option to the migration code. I grabbed the iv from the console and placed it in an env variable.
unencrypt_field :users, :date_of_birth, key: ENV["USER_DOB"], iv: ENV["USER_DOB_IV]

The result is the method not recognizing iv as a keyword.

I'm going to drop the traces in here as well. It might be overkill but better all the info than not enough.

Here is the trace below:

== 20170114141613 UnencryptFields: migrating ==================================
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

unknown keyword: iv/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/encryption_migrator-0.1.0/lib/encryption_migrator.rb:27:in `unencrypt_field'
/Users/jbrown/Desktop/Rails Workspace/PMS/db/migrate/20170114141613_unencrypt_fields.rb:3:in `up'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:611:in `exec_migration'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:592:in `block (2 levels) in migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:591:in `block in migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:590:in `migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:768:in `migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:998:in `block in execute_migration_in_transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:1044:in `block in ddl_transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/transactions.rb:220:in `transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:1044:in `ddl_transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:997:in `execute_migration_in_transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:959:in `block in migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:955:in `each'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:955:in `migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:823:in `up'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:801:in `migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
ArgumentError: unknown keyword: iv
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/encryption_migrator-0.1.0/lib/encryption_migrator.rb:27:in `unencrypt_field'
/Users/jbrown/Desktop/Rails Workspace/PMS/db/migrate/20170114141613_unencrypt_fields.rb:3:in `up'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:611:in `exec_migration'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:592:in `block (2 levels) in migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:591:in `block in migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:590:in `migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:768:in `migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:998:in `block in execute_migration_in_transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:1044:in `block in ddl_transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/transactions.rb:220:in `transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:1044:in `ddl_transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:997:in `execute_migration_in_transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:959:in `block in migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:955:in `each'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:955:in `migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:823:in `up'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:801:in `migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate

I tried to drop the string in without the iv keyword (both as a string and as an env variable) with no better results. Here is the trace:

rake aborted!
SyntaxError: /Users/jbrown/Desktop/Rails Workspace/PMS/db/migrate/20170114141613_unencrypt_fields.rb:3: syntax error, unexpected '\n', expecting =>
/Users/jbrown/Desktop/Rails Workspace/PMS/db/migrate/20170114141613_unencrypt_fields.rb:4: syntax error, unexpected '\n', expecting =>
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:244:in `rescue in load_dependency'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:237:in `load_dependency'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:777:in `load_migration'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:773:in `migration'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:768:in `disable_ddl_transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:1051:in `use_transaction?'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:1043:in `ddl_transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:997:in `execute_migration_in_transaction'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:959:in `block in migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:955:in `each'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:955:in `migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:823:in `up'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/migration.rb:801:in `migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/Users/jbrown/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.5/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate

Funny, it's screaming about an escape but when I escape the \n, I get the same result.

I did check out the https://github.com/attr-encrypted/encryptor/blob/master/lib/encryptor.rb#L45-L47 and see the iv option there, along with salt which seems to be deprecated.

@tomkadwill
Copy link
Owner

Thanks for the detailed report @jbrown296, it looks like my gem doesn't accept the iv keyword argument and therefore isn't passing it on to the attr-encrypted gem.

It requires a change here: https://github.com/tomkadwill/encryption_migrator/blob/master/lib/encryption_migrator.rb#L26-L40

And also one here: https://github.com/tomkadwill/encryption_migrator/blob/master/lib/encryption_migrator.rb#L8-L12

Would it be possible for you to create a sample application that re-produce your problem? That way I can easily fix the problem and test that it works with your sample app.

Alternatively you could try to change those methods to add in iv and create a PR if it works.

@jbrown296
Copy link
Author

Hi @tomkadwill, I threw up a quick app here: https://github.com/jbrown296/toy_app/

So, the sample app has the gem but does not have the "unencrypt" migration, obviously, because it won't migrate. The user model names are encrypted. Check it out and let me know what you think.

And then I read your entire message... I will take a look at where the changes could be made if I can get to it within the next couple of days.

@jbrown296
Copy link
Author

Hi @tomkadwill, I cannot seem to configure this properly. Any luck on your end?

@tomkadwill
Copy link
Owner

Hey @jbrown296, I haven't had time to look at this yet. I'll try to make some time this weekend

@tomkadwill
Copy link
Owner

Hi @jbrown296, I've had another look at this today. I've modified the code so that it accepts an IV but the problem I've got is that I don't know how to read the unencrypted IV. For example, this code works:

salt = Time.now.to_i.to_s
secret_key = 'secret'
iv = OpenSSL::Cipher::Cipher.new('aes-256-cbc').random_iv
encrypted_value = Encryptor.encrypt('some string to encrypt', :key => secret_key, :iv => iv, :salt => salt)
decrypted_value = Encryptor.decrypt(encrypted_value, :key => secret_key, :iv => iv, :salt => salt)

In this case, I know the iv value and I so I can decrypt the value. However, in your example app. I don't know the IV. My understanding is that the IV is assigned randomly each time. Here is some example code:

irb(main):046:0> iv = "dQ\xFBV\xF4\xB1X\x1Aj\x8F\xFB\xF1\xD7\v\xC0\xDD"
irb(main):047:0> User.create!(name: 'tom', email: '[email protected]', encrypted_name_iv: iv)
irb(main):048:0> User.decrypt(:name, User.first.read_attribute(:encrypted_name), key: key, iv: iv)
  User Load (0.3ms)  SELECT  "users".* FROM "users"  ORDER BY "users"."id" ASC LIMIT 1
OpenSSL::Cipher::CipherError:

Even if I try to assign the IV, I can't read the encrypted value, using the IV. As I said, I think this is because it's assigning a random IV.

Do you have any ideas about this? If not, I'll raise an issue on the attr-encrypted gem to see if they can help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants