using Org.BouncyCastle.Math; using Org.BouncyCastle.Math.EC; // Create an ECFieldElement object. ECFieldElement fieldElement = new FpFieldElement( new BigInteger("168528749932448275088586267186997887104326"); new BigInteger("168528749932448275088586267186997887104327")); // Convert to a BigInteger object. BigInteger bigInteger = fieldElement.ToBigInteger();
using System.Numerics; using System.Security.Cryptography; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Agreement; using Org.BouncyCastle.Math.EC; // Generate an ECDiffieHellman agreement. ECDiffieHellman ecdh = ECDiffieHellman.Create(); // Get the shared secret value. byte[] sharedSecret = ecdh.DeriveKeyMaterial(ecdh.PublicKey); // Convert the shared secret to an ECFieldElement object. BigInteger biSharedSecret = new BigInteger(sharedSecret); ECFieldElement fieldElement = ecdh.PublicKey.Parameters.Curve.FromBigInteger(biSharedSecret); // Convert the ECFieldElement to a BigInteger. BigInteger bigInteger = fieldElement.ToBigInteger();This example shows how to perform an ECDH key agreement using `ECDiffieHellman` and `Org.BouncyCastle.Crypto.Agreement`. We first generate an `ECDiffieHellman` object and get the shared secret value using the `DeriveKeyMaterial` method. We then convert the shared secret to an `ECFieldElement` object using the `Curve.FromBigInteger` method and convert it to a `BigInteger` object using `ToBigInteger`. Package library: `Org.BouncyCastle`.