I wanted to verify JWT signature with RS512 algorithm using public key. I fond the exact solution given in the below link and it is working perfectly.
Verifying JWT signed with the RS256 algorithm using public key in C#
But I want to use System.IdentityModel.Tokens.Jwt with my application. Can anyone change below working example by implementing System.IdentityModel.Tokens.Jwt ?
static void Main(string[] args)
{
var token = "eyJhbGciOiJSUzUxMiIsImtpZCI6ImsxMDY5NDgxOTAifQ.eyJleHAiOjE0NzMzNDcxODUsInN1YiI6ImZmZmZmZmZmNTcxZGJkNjBlNGIwMWYyNzk4ZGI5N2Y4Iiwic2Vzc2lkIjoiNzZlNTg4ZDIzZmM3NDBiMGFkNzIxMDk2MGYwOWFhY2IiLCJ0eXBlIjoiYXQiLCJpYXQiOjE0NzMzMzYzODV9.WA-5NFaDx38dDEbZTH_hEYpbhuC3yTA9RHCmyF3Z8L1eYmZ8w4RFv5PrjWN-HprkMP7WzVfwKeSCqU4O1_FGbl88arCgZb_Ui7VUxwftRDMErib8XFu4hGfRKrdZOOHxBY_EGLINLobYG-n0akRTycIjmH0sgroQ_3Na7sxCJSM";
var secretKey = "j6Dtct-hCbacNoaTWVskOLh7Fcj4snuQ2kY3ZIpOZfJP-fsBgj6dxUFiqZSKjHikk73xiVLAb6w2SqQ8Z2Ez5hpGmG0U3eZzWkm8gwrpN-DN3eSBjBzyE5UUSTxmfMXGIBZtlwGEmmameycvX8nCJLuF83nK7Q5OQd7MIWUw-_8";
bool isValied = false;
string[] tokenParts = token.Split('.');
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.ImportParameters(
new RSAParameters()
{
Modulus = FromBase64Url(secretKey),
Exponent = FromBase64Url("AQAB")
});
HashAlgorithm H = SHA512.Create();
byte[] hash = H.ComputeHash(Encoding.UTF8.GetBytes(tokenParts[0] + '.' + tokenParts[1]));
RSAPKCS1SignatureDeformatter rsaDeformatter = new RSAPKCS1SignatureDeformatter(rsa);
rsaDeformatter.SetHashAlgorithm(H.GetType().ToString());
if (rsaDeformatter.VerifySignature(hash, FromBase64Url(tokenParts[2])))
isValied = true;
}
static byte[] FromBase64Url(string base64Url)
{
string padded = base64Url.Length % 4 == 0
? base64Url : base64Url + "====".Substring(base64Url.Length % 4);
string base64 = padded.Replace("_", "/")
.Replace("-", "+");
return Convert.FromBase64String(base64);
}