Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract public and private key from RSA JWK?

I'm trying to sign some data with a JWK i've been provided with.

So far i've tried to do this with jwt.io, the header is

{ "alg" : "RS256", "typ" : "JWT" }

and the payload is

{ "iss" : "4@john" }

Now to sign this I need a public and a private key. I've been told to extract these from the JWK provided, but i only seem to be able to extract a public key from this.

I've used jwk-to-pem but when provided with the JWK it only puts out the public key. But to sign with RS256 i need a public and a private key, i thought the private key is embedded into the JWK but i can't seem to extract it.

So my question is, how to extract the public AND private key from the JWK?

The JWK looks like this:

"ServicePrincipalKey": {
    "k": null,
    "kid": "urn:service:john:doe:4",
    "kty": "RSA",
    "use": null,
    "n": "rT-...skQ",
    "e": "A...B",
    "x5t": null,
    "d": "CP9...bsQ",
    "p": "7dG...PDk",
    "q": "un4...oxk",
    "dp": "HdF...m4Xk",
    "dq": "XGN...PMk",
    "qi": "0es...UDI",
    "nbf": "0001-01-01T00:00:00",
    "exp": "0001-01-01T00:00:00"
}
like image 248
Red-ER Avatar asked Jun 18 '18 07:06

Red-ER


1 Answers

Found the answer for jwk-to-pem. There is an option to generate a private and public key.

on runkit i executed the following code:

    var jwkToPem = require("jwk-to-pem")

    var jwk = {
    "k": null,
    "kid": "urn:service:john:doe:4",
    "kty": "RSA",
    "use": null,
    "n": "rT-...skQ",
    "e": "A...B",
    "x5t": null,
    "d": "CP9...bsQ",
    "p": "7dG...PDk",
    "q": "un4...oxk",
    "dp": "HdF...m4Xk",
    "dq": "XGN...PMk",
    "qi": "0es...UDI",
    "nbf": "0001-01-01T00:00:00",
    "exp": "0001-01-01T00:00:00"
    }

    var publicPEM = jwkToPem(jwk);
    console.log(publicPEM);

    var options = {"private" : true} //important this will set jwkToPem to output the private key
    var privatePEM = jwkToPem(jwk, options);
    console.log(privatePEM);

This outputs a public and a private key into the console.

Now by filling in these public and private keys into jwt.io i was able to generate a JWT

like image 180
Red-ER Avatar answered Sep 23 '22 20:09

Red-ER