ComputeSecret Method
Computes a shared secret.
procedure ComputeSecret();
Remarks
This method computes a shared secret using Elliptic Curve Diffie Hellman (ECDH).
When this method is called the component will use the public key specified by PublicKey and the private key specified by Key to compute a shared secret, or secret agreement. The ComputeSecretKDF property specifies the Hash or HMAC algorithm that is applied to the raw secret. The resulting value is held by SharedSecret. The following properties are applicable when calling this method:
- Key (required)
- PublicKey (required)
- ComputeSecretKDF (optional)
See ComputeSecretKDF for details on advanced settings that may be applicable for the chosen algorithm.
Keys created with the ed25519 and ed448 algorithms are not supported when calling this method.
Compute Secret Example
//Create a key for Party 1
Ecc ecc1 = new Ecc();
ecc1.CreateKey("x25519");
string ecc1_priv = ecc1.Key.PrivateKey;
string ecc1_pub = ecc1.Key.PublicKey;
//Create a key for Party 2
Ecc ecc2 = new Ecc();
ecc2.CreateKey("x25519");
string ecc2_priv = ecc2.Key.PrivateKey;
string ecc2_pub = ecc2.Key.PublicKey;
//Note: the public keys must be exchanged between parties by some mechanism
//Create the shared secret on Party 1
ecc1.Reset();
ecc1.Key.PrivateKey = ecc1_priv; //Private key of this party
ecc1.RecipientKey.PublicKey = ecc2_pub; //Public key of other party
ecc1.UseHex = true; //Hex encodes the shared secret bytes for easier display/storage
ecc1.ComputeSecret();
Console.WriteLine(ecc1.SharedSecret);
//Create the shared secret on Party 2
ecc2.Reset();
ecc2.Key.PrivateKey = ecc2_priv; //Private key of this party
ecc2.RecipientKey.PublicKey = ecc1_pub; //Public key of other party
ecc2.UseHex = true; //Hex encodes the shared secret bytes for easier display/storage
ecc2.ComputeSecret();
Console.WriteLine(ecc2.SharedSecret); //This will match the shared secret created by ecc1.