Skip to content

Instantly share code, notes, and snippets.

@dzmitry-savitski
Created April 9, 2025 14:13
Show Gist options
  • Save dzmitry-savitski/caeef097b285609687d901db21768f36 to your computer and use it in GitHub Desktop.
Save dzmitry-savitski/caeef097b285609687d901db21768f36 to your computer and use it in GitHub Desktop.
Decrypt jwt
import { jwtDecrypt } from 'jose';
// Your base64-encoded 256-bit key (should decode to 32 bytes)
const base64Key = 'yourBase64EncodedKeyHere'; // example: "3q2+7w==..."
// Decode base64 to a Uint8Array
const key = Uint8Array.from(Buffer.from(base64Key, 'base64'));
// Your JWE compact token
const token = 'eyJalgIjoiZGlyIiwiZW5jIjoiQTI1NkdDTSJ9..<IV>.<ciphertext>.<tag>';
async function decrypt() {
const { payload, protectedHeader } = await jwtDecrypt(token, key);
console.log('๐Ÿ”“ Decrypted payload:', new TextDecoder().decode(payload));
console.log('๐Ÿ“„ Header:', protectedHeader);
}
decrypt().catch(err => console.error('Decryption failed:', err));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment