问题
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