简体   繁体   中英

c# how to convert a JWK to a RsaSecurityKey

I am trying to verify signatures against a public key, and i got that working using RSACryptoproviders etc. I set the public key (importing the key in PEM format, reading the rsa parameters and then creating an RsaSecurityKey) It all works, but now a client wants me to support not only the PEM format, but also de JWK format.

When I google, I find 100 sites converting PEM's to JWK's (there is a JwkConvert class even) but it only goes one way.

How in c# can I do it the other way around? so I can convert the JWK to a RsaSecurityKey?

I found this post: How to transform a jwk to public key over openssl But i'm hoping i don't have to write all that myself..

Also, i'm allready using BouncyCastle crypto library (I use it for importing the PEM format)

I was hoping I could retreive the RsaParameters from the JWK, but i'm just a user of crypto and the letters don't tell me anything.

to give a bit more information, this is my JWK:

{"keys":[{"kid":"OEbwVQck6qGhsCWiYenQowD9wHic7BtS-LALJEna-og","kty":"RSA","alg":"RS256","use":"sig","n":"w1Y2AZWK8k0gWPQv5Q_GnNHna8kD_TcZhB0_qM8cQ8ABJUiynLjS52tVLeo9NQnIBiiGDDby_bmAkJnxUtzanpGo4wnsvckjHFiThZSNnMpU_GIRFvhjyfPcnvzLhuvmd_gX9iDQmpJk2nbBoatkKwnaiAl0VQ8jP85DxQYsjlxwrKhhVqIe4EYEazq3zTpKOKWu4QSQYE74lvWbyqEmSPG32V2Te7L7BDVAgqsQhAqx0mRpMHcJPtgX0AsM5iQrxUow51Y22shd6pgLkgvbc8LfXKWHAUgeg20JBBQc_gReek1h0qToDY-hWdxLpGBOLeWUKoUVAzAj2gXgTER2TQ","e":"AQAB","x5c":["MIIClTCCAX0CBgFz4hEUBTANBgkqhkiG9w0BAQsFADAOMQwwCgYDVQQDDANJUkQwHhcNMjAwODEyMDk0NTM2WhcNMzAwODEyMDk0NzE2WjAOMQwwCgYDVQQDDANJUkQwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDDVjYBlYryTSBY9C/lD8ac0edryQP9NxmEHT+ozxxDwAElSLKcuNLna1Ut6j01CcgGKIYMNvL9uYCQmfFS3NqekajjCey9ySMcWJOFlI2cylT8YhEW+GPJ89ye/MuG6+Z3+Bf2INCakmTadsGhq2QrCdqICXRVDyM/zkPFBiyOXHCsqGFWoh7gRgRrOrfNOko4pa7hBJBgTviW9ZvKoSZI8bfZXZN7svsENUCCqxCECrHSZGkwdwk+2BfQCwzmJCvFSjDnVjbayF3qmAuSC9tzwt9cpYcBSB6DbQkEFBz+BF56TWHSpOgNj6FZ3EukYE4t5ZQqhRUDMCPaBeBMRHZNAgMBAAEwDQYJKoZIhvcNAQELBQADggEBAHrHDQWMgXLiumpq3JAKDjH+8bf6VRvG40xNRz6H0Xg3pdC+ME8Jqtl49z2oXLJPCDKqyX2Lv9YRn0tFP6VYkBLQp1BBg7j2y8frkMrhzoRt28dhMfyMtdFgOhykwaPl6V0ASmd/iMyjK52XYjyUj+kVFhBNe1dA9F5Ftw1tyfwY5EFWbLyoBLzH+oZxbZzWIdFdSE9hhxOsioIaOOfrpBkincvQOYf/0RJS9jrVM2bRNah0HmxrdkiZNwvszm2uHPVNzgNxiK1iNqTHztbNKm9uO7diziJ4I4f2fesSMEdZG8AZ52NwP1ZVcTE+ZXcOE/+NhyAVf+4SP4abpqzi8+Q="],"x5t":"oNVqosVxKIQ8TW3MGZzM8_2AIJQ","x5t#S256":"_VULg4UAhXE8V7vJFJDTUYeoH0FL2pbbK0Lc8-3CK3k"}]}

When i create a Microsoft JsonWebKey object out of it, the 'N' and the 'E' parameters are null (as are 'D', 'DP', 'DQ', 'P', 'Q', 'QI', 'X' and 'Y')

Therefor I can't create RsaParameters...

as it turns out it was an oopsie from my side.. the string i posted was a jsonWebKeySET and not just a single jsonwebkey. I put it in the constructor of the JsonWebKey class and got no parse error whatsoever (grrr microsoft) so assumed it was correct.

Turns out I had to use the JsonWebKeySet() class. @jps comment helped a lot after that. (don't know how to give you credit for that)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM