Rust Requires Chilkat v11.0.0+
Rust
How to Generate an Elliptic Curve Shared Secret
See more ECC Examples
Demonstrates how to generate an ECC (Elliptic Curve Cryptography) shared secret. Imagine a cilent has one ECC private key, the server has another. A shared secret is computed by each side providing it's public key to the other. The private keys are kept private.Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// This example includes both client-side and server-side code.
// Each code segment is marked as client-side or server-side.
// Imagine these segments are running on separate computers...
// -----------------------------------------------------------------
// (Client-Side) Generate an ECC key, save the public part to a file.
// -----------------------------------------------------------------
let prng_client = chilkat::Prng::new();
let ecc_client = chilkat::Ecc::new();
let priv_key_client = chilkat::PrivateKey::new();
if ecc_client.gen_key("secp256r1", &prng_client, &priv_key_client).is_err() {
println!("{}", ecc_client.last_error_text());
return;
}
let pub_key_client = chilkat::PublicKey::new();
let _ = priv_key_client.to_public_key(&pub_key_client);
let _ = pub_key_client.save_pem_file(false, "qa_output/eccClientPub.pem");
// -----------------------------------------------------------------
// (Server-Side) Generate an ECC key, save the public part to a file.
// -----------------------------------------------------------------
let prng_server = chilkat::Prng::new();
let ecc_server = chilkat::Ecc::new();
let priv_key_server = chilkat::PrivateKey::new();
let _ = ecc_server.gen_key("secp256r1", &prng_server, &priv_key_server);
let pub_key_server = chilkat::PublicKey::new();
let _ = priv_key_server.to_public_key(&pub_key_server);
let _ = pub_key_server.save_pem_file(false, "qa_output/eccServerPub.pem");
// -----------------------------------------------------------------
// (Client-Side) Generate the shared secret using our private key, and the other's public key.
// -----------------------------------------------------------------
// Imagine that the server sent the public key PEM to the client.
// (This is simulated by loading the server's public key from the file.
let pub_key_from_server = chilkat::PublicKey::new();
let _ = pub_key_from_server.load_from_file("qa_output/eccServerPub.pem");
let shared_secret1 = ecc_client.shared_secret_enc(&priv_key_client, &pub_key_from_server, "base64").unwrap_or_default();
// -----------------------------------------------------------------
// (Server-Side) Generate the shared secret using our private key, and the other's public key.
// -----------------------------------------------------------------
// Imagine that the client sent the public key PEM to the server.
// (This is simulated by loading the client's public key from the file.
let pub_key_from_client = chilkat::PublicKey::new();
let _ = pub_key_from_client.load_from_file("qa_output/eccClientPub.pem");
let shared_secret2 = ecc_server.shared_secret_enc(&priv_key_server, &pub_key_from_client, "base64").unwrap_or_default();
// ---------------------------------------------------------
// Examine the shared secrets. They should be the same.
// Both sides now have a secret that only they know.
// ---------------------------------------------------------
println!("{}", shared_secret1);
println!("{}", shared_secret2);