How to convert ECDiffieHellmanPublicKey instance to DER file in .net framework?

末鹿安然 提交于 2019-12-11 18:48:38

问题


I have following code:

ECDiffieHellman ecdh = ECDiffieHellman.Create(ECCurve.NamedCurves.nistP256);
ECDiffieHellmanPublicKey ecdhPublic = ecdh.PublicKey;

How can I export the ECDiffieHellmanPublicKey instance (ecdhPublic) into DER encoded file?


回答1:


Using BouncyCastle (http://www.bouncycastle.org/) you can export your ECDH public key to a DER encoded ANS.1 Object

X9ECParameters ecP = NistNamedCurves.GetByName("P-256");
ECDomainParameters ecSpec = new ECDomainParameters(ecP.Curve, ecP.G, ecP.N, ecP.H, ecP.GetSeed());
ECKeyPairGenerator g = new ECKeyPairGenerator();
g.Init(new ECKeyGenerationParameters(ecSpec, new SecureRandom()));

AsymmetricCipherKeyPair server = g.GenerateKeyPair();
ECPublicKeyParameters serverPub = (ECPublicKeyParameters)server.Public;
var result = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(serverPub).GetDerEncoded();


来源:https://stackoverflow.com/questions/48507687/how-to-convert-ecdiffiehellmanpublickey-instance-to-der-file-in-net-framework

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