A very precise question!
According to RFC 7517, JSON Web Key (JWK) specification, the answer is: base64url encoding, with no padding.
Here's the relevant excerpt from RFC 7517, Section 4.1:
4.1. "n" (Modulus) Parameter
The "n" (modulus) parameter contains the modulus value for the RSA public key. It is represented as a Base64urlUInt-encoded value.
And, if we dig deeper into the terminology:
Base64urlUInt encoding: The Base64url encoding of the value, with no padding (= characters removed), as defined in Section 2 of RFC4648.
Now, let's break it down:
- Base64url is a variant of Base64 encoding, defined in RFC 4648. The key differences between Base64 and Base64url are:
- + (plus) is replaced with - (minus)
- / (forward slash) is replaced with _ (underscore)
- UInt means that the value is an unsigned integer, represented as a big-endian byte array (most significant byte first).
- No padding means that the trailing = (equals) characters, used to pad the base64-encoded value to a multiple of 4 characters, are removed.
So, to summarize:
The value of n (Modulus) in a JWK must be:
- Converted to a big-endian byte array (UInt)
- Base64url-encoded (using
- and _ instead of + and /)
- Without padding (no trailing
= characters)
Example:
Suppose your RSA modulus n is a 256-byte big-endian value:
00 98 fa 42 13 ... 78 9a bc (hexdump)
When converted to base64url (using e.g., base64url command-line tool or a library), the encoded value might look like:
AZo6QhM4j...3LoqzA (no padding, no = at the end)
Not:
AZo6QhM4j...3LoqzA== (with padding, wrong)
AZo6QhM4j...3LoqzA/= (using / and padding, wrong)
AZo6QhM4j...3LoqzA+ (using + instead of -, wrong)
Thus, the precise answer is: the value of n should end with no padding (=) characters, using base64url encoding (with - and _ characters).
Hope this clears up any confusion!
Here's some sample Python code to illustrate this using cryptography and base64url libraries:
import os
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.backends import default_backend
import base64
# Generate a sample RSA keypair
key = serialization.load_pem_private_key(
open("path/to/your/rsa_private_key.pem", "rb").read(),
password=None,
backend=default_backend()
)
# Get the RSA public key
public_key = key.public_key()
# Extract the modulus (n)
n = public_key.public_numbers().n
# Convert n to bytes (big-endian)
n_bytes = n.to_bytes((n.bit_length() + 7) // 8, byteorder='big')
# Base64url encode n_bytes (no padding)
n_b64url = base64.urlsafe_b64encode(n_bytes).decode('ascii').rstrip("=")
print(n_b64url) # Your base64url-encoded modulus value (no padding)
Now, use n_b64url as the value for n in your JWK JSON:
{
"kty": "RSA",
"n": "AZo6QhM4j...3LoqzA",
"e": "AQAB"
}