[英]Rails: ActiveSupport::MessageEncryptor::InvalidMessage
[英]Rails 7 - ActiveSupport::MessageEncryptor
我將我的應用程序升級到 Rails 7(從 Rails 6.1)並遇到了一個問題。
我使用 ActiveSupport::MessageEncryptor 加密了一些數據字段。 我在下面有我的代碼來加密和解密。
創建新記錄時沒有問題。 這些值是加密的。 然后當我查看記錄時,值被解密。
問題是我無法查看在升級到 Rails 7 之前創建的任何記錄。 decrypt_and_verify 方法拋出 ActiveSupport::MessageEncryptor::InvalidMessage
請注意,ENV 值 KEY_GENERATOR_SECRET 和 KEY_GENERATOR_SALT 沒有改變。
我未能測試解密舊記錄並認為我很好 - 所以我已將其推向生產(在 Heroku 上)。 此問題存在於開發、測試和生產中。 美好的時光。
感謝您的幫助,查理
# ----------------------------------------------------------------
# encrypt a value
def encrypt_value(obj_value)
# create the key from a combination of
# our secret passcode + unique salt
key = ActiveSupport::KeyGenerator.new(ENV['KEY_GENERATOR_SECRET']).generate_key(ENV['KEY_GENERATOR_SALT'], 32)
# encrypt the objects value
ActiveSupport::MessageEncryptor.new(key).encrypt_and_sign(obj_value)
end
# ----------------------------------------------------------------
# decrypt a value
def decrypt_value(encrypted_value)
# if nothing to encrypt,
# return an empty string
return '' if encrypted_value.nil?
# create the key from a combination of
# our secret passcode + unique salt
key = ActiveSupport::KeyGenerator.new(ENV['KEY_GENERATOR_SECRET']).generate_key(ENV['KEY_GENERATOR_SALT'], 32)
# decrypt the objects encrypted value
ActiveSupport::MessageEncryptor.new(key).decrypt_and_verify(encrypted_value)
end
# ----------------------------------------------------------------
對於那些有同樣問題並遇到這篇文章的人......我想通了。
Rails 7 更新了默認摘要 class 以在主動支持中生成密鑰,從 SHA1 到 SHA256。 由於所有當前值都使用 SHA1 加密,這就是我需要使用的。
application.rb 中的以下行修復了它。
config.active_support.key_generator_hash_digest_class = OpenSSL::Digest::SHA1
下一個項目是將所有加密值從 SHA1 更新為 SHA256。
祝你好運! 查理
就我而言,我嘗試使用config.active_support.key_generator_hash_digest_class = OpenSSL::Digest::SHA1
但對我不起作用。
我的解決方案是..
# ----------------------------------------------------------------
# encrypt a value
def encrypt_value(obj_value)
# create the key from a combination of
# our secret passcode + unique salt
key = ActiveSupport::KeyGenerator.new(ENV['KEY_GENERATOR_SECRET'], hash_digest_class: OpenSSL::Digest::SHA1).generate_key(ENV['KEY_GENERATOR_SALT'], 32)
# encrypt the objects value
ActiveSupport::MessageEncryptor.new(key).encrypt_and_sign(obj_value)
end
# ----------------------------------------------------------------
# decrypt a value
def decrypt_value(encrypted_value)
# if nothing to encrypt,
# return an empty string
return '' if encrypted_value.nil?
# create the key from a combination of
# our secret passcode + unique salt
key = ActiveSupport::KeyGenerator.new(ENV['KEY_GENERATOR_SECRET'], hash_digest_class: OpenSSL::Digest::SHA1
).generate_key(ENV['KEY_GENERATOR_SALT'], 32)
# decrypt the objects encrypted value
ActiveSupport::MessageEncryptor.new(key).decrypt_and_verify(encrypted_value)
end
# ----------------------------------------------------------------
如果我在ActiveSupport::KeyGenerator.new
中添加hash_digest_class: OpenSSL::Digest::SHA1
對我有用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.