| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- using System.Security.Cryptography;
- using Microsoft.IdentityModel.Tokens;
- namespace Passwordless;
- public static class KeyConverter
- {
- public static RSA JwkToRsa(JsonWebKey key)
- {
- var rsaParameters = new RSAParameters
- {
- // PUBLIC KEY PARAMETERS
- // n parameter - public modulus
- Modulus = Base64UrlEncoder.DecodeBytes(key.N),
- // e parameter - public exponent
- Exponent = Base64UrlEncoder.DecodeBytes(key.E),
-
- // PRIVATE KEY PARAMETERS (optional)
- // d parameter - the private exponent value for the RSA key
- D = Base64UrlEncoder.DecodeBytes(key.D),
- // dp parameter - CRT exponent of the first factor
- DP = Base64UrlEncoder.DecodeBytes(key.DP),
- // dq parameter - CRT exponent of the second factor
- DQ = Base64UrlEncoder.DecodeBytes(key.DQ),
- // p parameter - first prime factor
- P = Base64UrlEncoder.DecodeBytes(key.P),
- // q parameter - second prime factor
- Q = Base64UrlEncoder.DecodeBytes(key.Q),
- // qi parameter - CRT coefficient of the second factor
- InverseQ = Base64UrlEncoder.DecodeBytes(key.QI)
- };
- return RSA.Create(rsaParameters);
- }
- public static JsonWebKey ExtractPublicKey(JsonWebKey key)
- {
- return new JsonWebKey()
- {
- Kty = key.Kty,
- E = key.E,
- N = key.N,
- Use = "sig",
- };
- }
- }
|