Get DER-encoded public key

元气小坏坏 提交于 2021-02-07 19:39:18

问题


Using BounceCastle I have the following code working. It generates a keypair and return the ASN.1 DER-encoded format.

//Generate new key
var generator = new RsaKeyPairGenerator ();
generator.Init (new KeyGenerationParameters (new SecureRandom (), 1024));
var keyPair = generator.GenerateKeyPair ();

//Save private key for later use
keyParameters = (RsaKeyParameters)keyPair.Private;

//Export ASN.1 DER-encoded
SubjectPublicKeyInfo info = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(keyPair.Public);
return info.GetEncoded ();

It has been tested and works with third party software.

My question is: how can I make the inverse of the above encoding. Having the encoded public key, how do I get the public key into a RsaKeyParameters.

I guess I'm about to do something similar to this.

SubjectPublicKeyInfo s = new SubjectPublicKeyInfo(????, publicKeyBytes);
RsaKeyParameters key = (RsaKeyParameters)PublicKeyFactory.CreateKey(s);

So if this is close I need to know what to put in ????, it expects an object of type AlgorithmIdentifier.


回答1:


Thanks to this answer I got the following code:

        AsymmetricKeyParameter asymmetricKeyParameter = PublicKeyFactory.CreateKey(req.PublicKey);
        RsaKeyParameters key = (RsaKeyParameters) asymmetricKeyParameter;


来源:https://stackoverflow.com/questions/10963756/get-der-encoded-public-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!