Skip to content

Instantly share code, notes, and snippets.

@kenkoooo
Created September 4, 2023 07:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenkoooo/40828c99d5b18deaa718fb3905532b15 to your computer and use it in GitHub Desktop.
Save kenkoooo/40828c99d5b18deaa718fb3905532b15 to your computer and use it in GitHub Desktop.
Decrypt the Rails session cookie in Node.js
function decrypt_session(cookie) {
const SALT = "authenticated encrypted cookie";
const secret = crypto.pbkdf2Sync(secret_key_base, SALT, 1000, 32, 'sha1');
const [data, iv, auth_tag] = cookie.split('--').map(s => Buffer.from(s, 'base64'));
const decipher = crypto.createDecipheriv('aes-256-gcm', secret, iv);
decipher.setAuthTag(auth_tag);
let decrypted_data = decipher.update(data);
decrypted_data = Buffer.concat([decrypted_data, decipher.final()]);
console.log(decrypted_data.toString());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment