0

实际上,我从user_pass使用硬编码密钥加密字段开始。

class Credential < ApplicationRecord
  ..
  attr_encrypted :user_pass, key: 'This is a key that is 256 bits!!'
  ..
end

我已经用这个密钥加密了一些数据。现在,我不想以硬编码格式保存密钥,因此将一半密钥保存在文件系统中,另一半保存在表中并将它们组合起来。

class Credential < ApplicationRecord
  ..
  attr_encrypted :user_pass, key: :encryption_key
  ..

  def encryption_key
    Rails.root.join('private', 'key').read + Setting.where(name: 'key').last.value
  end
end

如何使用当前密钥加密已经加密的数据?

4

1 回答 1

1

您可以做的是使用新键编写另一个字段:

attr_encrypted :user_pass, key: 'This is a key that is 256 bits!!'
attr_encrypted :user_pass2, key: :encryption_key

然后就可以迁移数据了。

credential.user_pass2 = user.user_pass
credential.save

完成此迁移后,您可以将其他代码指向新字段。或者删除/重命名旧的并将 user_pass2 重命名为 user_pass (以便其他代码继续工作)。

于 2018-10-02T12:57:17.090 回答