I'm trying to create an instance of System.Security.Cryptography.RSA
from a JSON Web Key Set (JWKS) that includes some RSA keys, but only includes their modulus (n), public exponent (e) and secret exponent (d), not the primes that were used during key generation (p and q).
This is the jwks that includes the private keys (test keys, of course): https://belgianmobileid.github.io/slate/private_jwks.json
Here's what I tried:
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using Microsoft.IdentityModel.Tokens;
class Program
{
static void Main(string[] args)
{
var jwks = new WebClient().DownloadString("https://belgianmobileid.github.io/slate/private_jwks.json");
var webKeySet = JsonWebKeySet.Create(jwks);
// signing key has kid "s1"
var signingkey = webKeySet.Keys.Single(key => key.KeyId == "s1");
// throws System.Security.Cryptography.CryptographicException:
// 'The specified RSA parameters are not valid; both Exponent and Modulus are required fields.'
var rsa = RSA.Create(
new RSAParameters
{
Modulus = Base64UrlEncoder.DecodeBytes(signingkey.N),
Exponent = Base64UrlEncoder.DecodeBytes(signingkey.E),
D = Base64UrlEncoder.DecodeBytes(signingkey.D),
P = null, // unknown
Q = null // unknown
});
}
}
I'm certainly no cryptography expert, I'm just trying to parse the JWKS into something that I can use in code. Is this even possible without knowing p or q?