I have been looking for a way in C# to decrypt JWE tokens encrypted with ECDH-ES+A128KW by my OpenId Connect provider. I have shared the EC public key with OIDC provider and they send me a JWE encrypted using the shared public key. I want to decrypt and extract information from JWE payload using the private key which I have stored securely. I have been looking for a proper decryption library in C# for a while. My application is a .Net 5 application and runs on Linux. Any help highly appreciated.
Asked
Active
Viewed 694 times
0
-
Personally, I'd use a third party package like [jose-jwt](https://www.nuget.org/packages/jose-jwt/) ([GitHub](https://github.com/dvsekhvalnov/jose-jwt)) – MindSwipe Jun 28 '22 at 08:45
-
@MindSwipe thank you. I managed to do it with jose-jwt – Rama Jul 19 '22 at 09:08
1 Answers
0
Answering this question assuming it would help someone. You can decrypt the JWS using the following code
using Jose;
var privateKey = new Jwk(
crv: encryptionPublicKey.crv,
x: "y_0F1OlwlIj0VVDbWF2D3JnHqryJK58CExQXqJr3e5s",
y: "Av4X6ew_hQLmL3qgJJjKcqJcTftpsDk0VLwFLBEzEIE",
d: "k44uec9XUofhcGUD6mwf-1krn4nQJ5q3TWDwg8wkTFY",
);
string decryptedToken = JWT.Decode(token, privateKey);
If you still get a JWT as a decryptedToken, you can read the token using following method
JwtSecurityTokenHandler handler = new();
var jwtSecToken = handler.ReadJwtToken(decryptedIdToken);

Rama
- 187
- 1
- 6
- 20