JWT encoder
Write a JSON payload, pick an algorithm, and sign it with a secret or a private key to get a JSON Web Token for testing an API. Your server accepts the token only if it checks it with the same algorithm and the same secret, or with the public key that matches your private key. It runs in your browser; nothing you enter is uploaded.
For the reverse, use the JWT decoder.
More options
Signing with a shared secret
For HS256, HS384 and HS512, type the secret your server verifies tokens with. The tool signs with the secret's characters as UTF-8 text,
which is how jsonwebtoken for Node.js and PyJWT for Python treat a secret given as a string. Some setups decode the secret
from Base64 first, such as Java code that passes Decoders.BASE64.decode(secret) to jjwt. For a secret like that, tick
The secret is Base64-encoded under More options. Read the other way, the same secret gives different bytes, and the server
rejects the signature.
RFC 7518 requires an HMAC key at least as long as the
hash: 32 bytes for HS256, 48 for HS384 and 64 for HS512. The tool warns when a secret is shorter, because anyone holding one token can try
guesses against it offline, and a guessed secret lets them sign tokens your server accepts. Generate a new secret makes
one from that many random bytes, written in URL-safe Base64: 43 characters for HS256. The tool uses those characters as plain text, so you
can paste the secret into your server's config as it is. In a terminal, openssl rand -base64 32 makes a similar one.
Signing with a private key
The RS, PS and ES algorithms sign with a private key, and the service that checks tokens needs only the matching public key. Paste the
private key as a JWK that includes its private part, "d", or as a PEM that starts with
-----BEGIN PRIVATE KEY-----, the PKCS #8 format, which is the only PEM format browsers read. A key that starts with
BEGIN RSA PRIVATE KEY or BEGIN EC PRIVATE KEY converts with
openssl pkcs8 -topk8 -nocrypt -in key.pem.
Generate a new key pair makes a 2048-bit RSA key, or an EC key on the algorithm's curve: P-256 for ES256, P-384 for ES384
and P-521 for ES512. The public key then appears under the token as a BEGIN PUBLIC KEY PEM, ready for the verifying service.
The tool shows it only for keys it generated; for a key you pasted, openssl pkey -in key.pem -pubout prints it. One RSA key
works with every RS and PS algorithm, while each ES algorithm needs its own curve, so switching between ES algorithms replaces a generated
pair.
Expiry, issue time and key ID
The tool puts alg and "typ": "JWT" in the header and writes the payload as JSON without spaces. The
Expires menu sets exp to that long after your last edit and replaces any exp in the payload, so
the token's time starts running before you copy it. As set in the payload keeps your own value, and a payload with no
exp gives a token that never expires. To write a date as seconds, use the
Unix timestamp converter.
Under More options, a checkbox adds the current time as iat, and Key ID adds a kid to the
header. A server that holds several keys, such as one that publishes a JWKS, reads kid to pick the key to verify with.
Creating tokens in code
-
Node.js:
jwt.sign({ sub: '123' }, secret, { expiresIn: '1h' })fromjsonwebtokensigns with HS256 unless you passalgorithm. -
Python:
jwt.encode({"sub": "123"}, secret, algorithm="HS256")from PyJWT. RS, PS and ES keys needpip install "pyjwt[crypto]". - Java and Spring Boot: Spring Security's
NimbusJwtEncoder, or the jjwt library. - C#:
JsonWebTokenHandler.CreateTokenin the Microsoft.IdentityModel.JsonWebTokens package.
Questions
Why does my server say the signature is invalid?
The server is checking with a different key or algorithm than the token was signed with. The usual causes are a secret read as text on one
side and as Base64 on the other, a space or line break copied along with the secret, or a different algorithm from the one the server
expects. Paste the token into the JWT decoder to check its alg and claims.
Why do I get a different token from the same input?
An expiry or iat writes the current time into the payload at each edit. PS and ES signatures also use a new random number
every time, so they differ even for identical input, and each one verifies. HS and RS give the same token for the same input.