Reputation: 11
I have a key in the format
-----BEGIN PRIVATE KEY-----
-----END PRIVATE KEY-----
I want to convert it to an RSA key to create a JWT from it. I'm using .NET 3.5 Framework.
But when I Use this code
PemReader pemReader = new PemReader(new StringReader(pem));
object key = pemReader.ReadObject();
//rsapri
RSAParameters rsaParameters = isPrivate ?
DotNetUtilities.ToRSAParameters((RsaPrivateCrtKeyParameters)(((AsymmetricCipherKeyPair)key).Private)) :
DotNetUtilities.ToRSAParameters((RsaKeyParameters)key);
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.ImportParameters(rsaParameters);
return rsa;
I get the error as
InvalidCastException: 'Unable to cast object of type 'Org.BouncyCastle.Crypto.Parameters.RsaPrivateCrtKeyParameters' to type 'Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair'.'
I realized since the Private Key that I have is in PKCS8 and not PKCS1 format. Is there a way to convert the PKCS8 to RSA?
Upvotes: 0
Views: 183