Reading PEM RSA Public Key Only using Bouncy Castle

前端 未结 6 1865
孤独总比滥情好
孤独总比滥情好 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:14

    Instead of:

    keyPair = (Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair)new Org.BouncyCastle.OpenSsl.PemReader(reader).ReadObject();
    

    Use:

    keyPair = (Org.BouncyCastle.Crypto.AsymmetricKeyParameter)new Org.BouncyCastle.OpenSsl.PemReader(reader).ReadObject();
    

    Since you are only using a public key and you don't actually have a pair of keys (public & private) you can't cast it as 'AsymmetricCipherKeyPair' you should cast it as 'AsymmetricKeyParameter'.

提交回复
热议问题