Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
jwt+rsa+dotnet with pem
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Security.Cryptography;
using Newtonsoft.Json;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Security;
namespace jwt
{
class Program
{
private const string PrivateKey = @"-----BEGIN RSA PRIVATE KEY-----
MIIEpgIBAAKCAQEA6OOvYVVd4vq4Q2K1SPOZ9IBe36QDAqrMK5e0DkmJlhy9Y29E
WfRlhEzt5+2IJU+SuKQojoNY3U2p4BA7KYbvH2DwCuxtTUXjP44fych3qtfMmnoM
wPzmgK1afYcTs/cM4JtYtdpEGSTst1mRH+Als8W2qc9MH2SFeQe3H/SmSF8KBrfO
zDtAHCRG0+vPTJcCFdmsyQfSLigwFsUULWkxNdtGp7i7z/cMdJ1fq/dHDaB34EEz
4ye7B/XeQlj0jlO5cOUco22Kne29Wp3vXQUbKHRGD8mXMBGBoBVOFxx8aKoXqoIm
QkLwOM55uhreOaEJDfjHnThonxyJguMmY2KfkwIDAQABAoIBAQDhQol6nfWn/FaK
Z0lt/KHnStt5ecynocjGSYARabO5DKdrLE5yq+AdbcOX17seS69hBPdtkUI9HEe5
wDlZW3ijY0ILbs/yNC0wZin9JPgKuZzftjnr2TKjOAoU7Lh0E/vSrK/LhgjkQUDt
v6aNrGMLVavu5+F7Y+FuRbYtJJhJ2jq4UmUIgxinvBaH7QdAxPLFgupaVFjOtVQ6
ajjZyjByHYg4ouBLIHez1PfeDPTLZkeUl/ULBIow96HsbJkqzgeTl9uV+1bNy4xO
dJJHuF61IZiEbh4plB4E2u0m8mblS32A+PbVCCzg4x29H90W5uc7YvANRveJ7Rkv
aVhIYcixAoGBAP6DIYEdPM26pSgVYkfPMLDdc4yq01X5GSJVmW1MozfI6qmm8kHp
+ibajMzwhXGARR6OZMxORDp3JheqzBxRwYBzS95z8t9Dh7vCFRbxEzc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QfSLigwFsUULWkx
NdtGp7i7z/cMdJ1fq/dHDaB34EEz4ye7B/XeQlj0jlO5cOUco22Kne29Wp3vXQUb
KHRGD8mXMBGBoBVOFxx8aKoXqoImQkLwOM55uhreOaEJDfjHnThonxyJguMmY2Kf
kwIDAQAB
-----END PUBLIC KEY-----
";
static void Main(string[] args)
{
var claims = new Dictionary<string, object>
{
{"sub", "1234567890"},
{"name", "John Doe"},
{"admin", true},
};
var jwt = GetJwt(claims, PrivateKey);
Debug.WriteLine("===JWT===");
Debug.WriteLine(jwt);
Debug.WriteLine("");
var claims2 = GetClaims(jwt, PublicKey);
Debug.WriteLine("===CLAIMS2===");
Debug.WriteLine(JsonConvert.SerializeObject(claims2));
Debug.WriteLine("");
}
static string GetJwt(Dictionary<string, object> claims, string privateKey)
{
using (var rsa = GetRSACryptoServiceProvider(privateKey,true))
{
var jwt = Jose.JWT.Encode(claims, rsa, Jose.JwsAlgorithm.RS256);
return jwt;
}
}
static RSACryptoServiceProvider GetRSACryptoServiceProvider(string key, bool isPrivate)
{
var rsaParams = GetRsaParams(key, isPrivate);
var rsa = new RSACryptoServiceProvider();
rsa.ImportParameters(rsaParams);
return rsa;
}
static RSAParameters GetRsaParams(string key, bool isPrivate)
{
if (isPrivate)
{
AsymmetricCipherKeyPair keyPair;
using (var sr = new StringReader(key))
{
PemReader pr = new PemReader(sr);
keyPair = (AsymmetricCipherKeyPair) pr.ReadObject();
}
return DotNetUtilities.ToRSAParameters((RsaPrivateCrtKeyParameters) keyPair.Private);
}
AsymmetricKeyParameter keyParam;
using (var sr = new StringReader(key))
{
PemReader pr = new PemReader(sr);
keyParam = (AsymmetricKeyParameter)pr.ReadObject();
}
return DotNetUtilities.ToRSAParameters((RsaKeyParameters)keyParam);
}
static Dictionary<string, object> GetClaims(string jwt, string publicKey)
{
using (var rsa = GetRSACryptoServiceProvider(publicKey, false))
{
var payload = Jose.JWT.Decode<Dictionary<string, object>>(jwt, rsa);
return payload;
}
}
}
}
@venkatdev22

This comment has been minimized.

Copy link

@venkatdev22 venkatdev22 commented Apr 16, 2019

Getting error in line Debug.WriteLine(JsonConvert.SerializeObject(claims2));
"Input string not in proper format...."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment