获取DER编码的公钥 [英] Get DER-encoded public key

查看:78
本文介绍了获取DER编码的公钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用BounceCastle,我可以使用以下代码.它会生成密钥对并返回ASN.1 DER编码的格式.

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.

我的问题是:如何使上述编码相反.具有已编码的公钥,如何将公钥放入RsaKeyParameters.

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);

因此,如果这很接近,我需要知道在????中放入什么,它会期望一个类型为AlgorithmIdentifier的对象.

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

推荐答案

感谢此答案我得到了以下代码:

Thanks to this answer I got the following code:

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

这篇关于获取DER编码的公钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆