Reading PEM RSA Public Key Only using Bouncy Castle

前端 未结 6 1889
孤独总比滥情好
孤独总比滥情好 2020-12-24 00:50

I am trying to use C# to read in a .pem file that contains only a RSA public key. I do not have access to the private key information, nor does my application

6条回答
  •  渐次进展
    2020-12-24 01:07

    EDIT: It looks like this depends on what type of key file you are using. For ssh-keygen keys, the private key appears to have a type of AsymmetricCipherKeyPair, but for openssl keys, the private key has a type of RsaPrivateCrtKeyParameters.


    Bryan Jyh Herng Chong's answer no longer appears to work for me (at least with Bouncy Castle version v1.8.5). It appears kp.GetType().GetProperty("Private") is no longer set differently for public vs private key PEM objects. It also appears that the object returned using PemReader.ReadObject() is now directly a RsaPrivateCrtKeyParameters object, so there's no longer a need to cast through a AsymmetricCipherKeyPair object first.

    I changed that line to this and it worked like a charm:

    return (kp.GetType() == typeof(RsaPrivateCrtKeyParameters)) ? MakePrivateRCSP(rsaKey, (RsaPrivateCrtKeyParameters)kp)) : MakePublicRCSP(rsaKey, (RsaKeyParameters)kp);
    

提交回复
热议问题