Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Decrypt Rails 5.2 session cookies
require 'cgi'
require 'active_support'
def verify_and_decrypt_session_cookie(cookie, secret_key_base = Rails.application.secret_key_base)
cookie = CGI::unescape(cookie)
salt = 'authenticated encrypted cookie'
encrypted_cookie_cipher = 'aes-256-gcm'
serializer = ActiveSupport::MessageEncryptor::NullSerializer
key_generator =, iterations: 1000)
key_len = ActiveSupport::MessageEncryptor.key_len(encrypted_cookie_cipher)
secret = key_generator.generate_key(salt, key_len)
encryptor =, cipher: encrypted_cookie_cipher, serializer: serializer)
Copy link

wildjcrt commented Jan 19, 2019

Copy link

inopinatus commented Jun 28, 2019

@wildjcrt Thank you. I wish you prosperity and a long life.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment