public DiffieHellmanKeyExchange(TlsContext ctx) { this.protocol = ctx.NegotiatedProtocol; switch (protocol) { case TlsProtocolCode.Tls12: Signature = new SignatureTls12(ctx.Session.ServerSignatureAlgorithm); break; case TlsProtocolCode.Tls10: Signature = new SignatureTls10(); break; case TlsProtocolCode.Tls11: Signature = new SignatureTls11(); break; default: throw new NotSupportedException(); } dh = new DiffieHellmanManaged(); Y = dh.CreateKeyExchange(); var dhparams = dh.ExportParameters(true); P = dhparams.P; G = dhparams.G; using (var buffer = CreateParameterBuffer(ctx.HandshakeParameters)) Signature.Create(buffer, ctx.Configuration.PrivateKey); }
public static void Main(string[] args) { // create a new DH instance DiffieHellman dh1 = new DiffieHellmanManaged(); // export the public parameters of the first DH instance DHParameters dhp = dh1.ExportParameters(false); // create a second DH instance and initialize it with the public parameters of the first instance DiffieHellman dh2 = new DiffieHellmanManaged(dhp.P, dhp.G, 160); // generate the public key of the first DH instance byte[] ke1 = dh1.CreateKeyExchange(); // generate the public key of the second DH instance byte[] ke2 = dh2.CreateKeyExchange(); // let the first DH instance compute the shared secret using the second DH public key byte[] dh1k = dh1.DecryptKeyExchange(ke2); // let the second DH instance compute the shared secret using the first DH public key byte[] dh2k = dh2.DecryptKeyExchange(ke1); // print both shared secrets to verify they are the same Console.WriteLine("Computed secret of instance 1:"); PrintBytes(dh1k); Console.WriteLine("\r\nComputed secret of instance 2:"); PrintBytes(dh2k); Console.WriteLine("\r\nPress ENTER to continue..."); Console.ReadLine(); }
public override KeyPair GenerateKeyPair() { DiffieHellmanManaged dh = new DiffieHellmanManaged(pspec.P.GetBytes(), pspec.G.GetBytes(), 0); DHParameters dhpars = dh.ExportParameters(true); BigInteger y = new BigInteger(dh.CreateKeyExchange()); return(new KeyPair(new DHPrivateKey(dhpars), new DHPublicKey(y))); }
public static DHKeyPair GenerateKeyPair(int keysize) { DiffieHellman dh = new DiffieHellmanManaged(_prime, new byte[] { 0x02 }, keysize); DHParameters privateKey = dh.ExportParameters(true); return(new DHKeyPair( new DHPrivateKey(privateKey), new DHPublicKey(dh.CreateKeyExchange()))); }
void CreateServer(TlsContext ctx) { dh = new DiffieHellmanManaged(); Y = dh.CreateKeyExchange(); var dhparams = dh.ExportParameters(true); P = dhparams.P; G = dhparams.G; using (var buffer = CreateParameterBuffer(ctx.HandshakeParameters)) Signature = SignatureHelper.CreateSignature(SignatureAlgorithm, buffer, ctx.Configuration.PrivateKey); }
void InitDiffieHellman(NativeOpenSslProtocol protocol) { var dh = new DiffieHellmanManaged(); var dhparams = dh.ExportParameters(true); openssl.SetDhParams(dhparams.P, dhparams.G); // Optional: this is OpenSsl's default value. if (protocol == NativeOpenSslProtocol.TLS12) { openssl.SetNamedCurve("prime256v1"); } }
public static void Main(string[] args) { DiffieHellman diffieHellman = new DiffieHellmanManaged(); DHParameters dHParameters = diffieHellman.ExportParameters(includePrivate: false); DiffieHellman diffieHellman2 = new DiffieHellmanManaged(dHParameters.P, dHParameters.G, 160); byte[] keyEx = diffieHellman.CreateKeyExchange(); byte[] keyEx2 = diffieHellman2.CreateKeyExchange(); byte[] bytes = diffieHellman.DecryptKeyExchange(keyEx2); byte[] bytes2 = diffieHellman2.DecryptKeyExchange(keyEx); Console.WriteLine("Computed secret of instance 1:"); PrintBytes(bytes); Console.WriteLine("\r\nComputed secret of instance 2:"); PrintBytes(bytes2); Console.WriteLine("\r\nPress ENTER to continue..."); Console.ReadLine(); }
public void KeyExchange() { // create a new DH instance DiffieHellman dh1 = new DiffieHellmanManaged(); // export the public parameters of the first DH instance DHParameters dhp = dh1.ExportParameters(false); // create a second DH instance and initialize it with the public parameters of the first instance DiffieHellman dh2 = new DiffieHellmanManaged(dhp.P, dhp.G, 160); // generate the public key of the first DH instance byte[] ke1 = dh1.CreateKeyExchange(); // generate the public key of the second DH instance byte[] ke2 = dh2.CreateKeyExchange(); // let the first DH instance compute the shared secret using the second DH public key byte[] dh1k = dh1.DecryptKeyExchange(ke2); // let the second DH instance compute the shared secret using the first DH public key byte[] dh2k = dh2.DecryptKeyExchange(ke1); // both shared secrets are the same AssertEquals("Shared Secret", dh1k, dh2k); }