ECDH Key Exchange Online | Elliptic Curve Diffie-Hellman Browser

Key Exchange
100% client-side. Processed locally in your browser using the Web Crypto API. Nothing is sent to any server.
⚠️ Critical Security: Never use the raw shared secret directly as an encryption key — always hash it with SHA-256 or use HKDF first. ECDH alone provides no authentication; add HMAC or digital signatures to prevent man-in-the-middle attacks. For post-quantum security, consider ML-KEM (FIPS 203).

How It Works

What Is ECDH Key Exchange?

ECDH (Elliptic Curve Diffie-Hellman) is a key agreement protocol that lets two parties — traditionally called Alice and Bob — establish a shared secret over an insecure channel without ever transmitting the secret itself. Each party generates a key pair on the same elliptic curve, exchanges public keys, and independently computes the same shared secret using their own private key and the other party's public key.

This tool demonstrates the full ECDH workflow entirely in your browser. Supports P-256, P-384, P-521, secp256k1, and X25519 curves. Includes a live AES-256-GCM message encryption demo to verify both parties derived identical material, and key import (PKCS#8/hex) for real exchanges. All computation runs locally — no key material is transmitted to any server.

When to Use ECDH

  • TLS 1.3 handshake — ECDH (especially X25519) is the default key exchange for modern TLS
  • Secure messaging — Signal Protocol uses X25519 for its double ratchet key agreement
  • SSH key exchange — X25519 is the default curve for SSH key exchange
  • End-to-end encryption — Any scenario where two parties need to establish a shared secret over an insecure channel
  • Hybrid key agreement — Combine X25519 with ML-KEM-768 for quantum-resistant key exchange
  • When NOT to use — For encrypting data to yourself or storing data, use symmetric encryption (AES-256-GCM) directly

Curve Comparison

CurveKey SizeSecurity LevelRSA EquivalentBest for
P-256256-bit128-bit~3072-bitTLS, JWT, general-purpose
P-384384-bit192-bit~7680-bitGovernment, payment
P-521521-bit256-bit~15360-bitMaximum security
secp256k1256-bit128-bit~3072-bitBitcoin, Ethereum
X25519256-bit128-bit~3072-bitTLS 1.3, SSH (recommended)

Standards Compliance

  • RFC 7748 — Elliptic Curves for Security (X25519, X448)
  • RFC 8446 (TLS 1.3) — ECDH key exchange for modern TLS
  • NIST SP 800-56A — Key Agreement Using Discrete Logarithm Cryptography
  • RFC 6090 — Fundamental Elliptic and Elliptic Curve Cryptography
  • Web Crypto API — W3C standard for browser-based ECDH operations

Last updated:

Code Examples

Perform ECDH key agreement in multiple languages:

JavaScript — Web Crypto API (Browser)

// P-256 ECDH key agreement
const aliceKey = await crypto.subtle.generateKey(
  { name: 'ECDH', namedCurve: 'P-256' }, true, ['deriveKey']
);
const bobKey = await crypto.subtle.generateKey(
  { name: 'ECDH', namedCurve: 'P-256' }, true, ['deriveKey']
);

// Alice derives shared secret using Bob's public key
const sharedSecret = await crypto.subtle.deriveKey(
  { name: 'ECDH', public: bobKey.publicKey },
  aliceKey.privateKey,
  { name: 'AES-GCM', length: 256 },
  true, ['encrypt', 'decrypt']
);
console.log('Shared key derived successfully');

Python — cryptography library

from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.primitives import hashes

alice_private = ec.generate_private_key(ec.SECP256R1())
bob_private = ec.generate_private_key(ec.SECP256R1())

# Alice derives shared secret using Bob's public key
shared_key = alice_private.exchange(ec.ECDH(), bob_private.public_key())
print(f"Shared secret: {shared_key.hex()}")

Go — crypto/ecdh

import "crypto/ecdh"

alice, _ := ecdh.P256().GenerateKey(rand.Reader)
bob, _ := ecdh.P256().GenerateKey(rand.Reader)

// Both derive the same shared secret
secret1, _ := alice.ECDH(bob.PublicKey())
secret2, _ := bob.ECDH(alice.PublicKey())
// secret1 == secret2

Rust — x25519-dalek

use x25519_dalek::{EphemeralSecret, PublicKey};
use rand::rngs::OsRng;

let alice_secret = EphemeralSecret::random_from_rng(OsRng);
let alice_public = PublicKey::from(&alice_secret);

let bob_secret = EphemeralSecret::random_from_rng(OsRng);
let bob_public = PublicKey::from(&bob_secret);

// Both derive the same shared secret
let alice_shared = alice_secret.diffie_hellman(&bob_public);
let bob_shared = bob_secret.diffie_hellman(&alice_public);

Node.js — crypto module

const { createECDH } = require('crypto');

const alice = createECDH('prime256v1');
const bob = createECDH('prime256v1');

alice.generateKeys();
bob.generateKeys();

// Both derive the same shared secret
const aliceSecret = alice.computeSecret(bob.getPublicKey());
const bobSecret = bob.computeSecret(alice.getPublicKey());
console.log('Match:', aliceSecret.equals(bobSecret));

Common Pitfalls

⚠️ Using Raw Shared Secret as Encryption Key: The raw ECDH output has subtle statistical biases. Always hash it with SHA-256 or use HKDF with salt and context before using it as an encryption key.
⚠️ No Authentication: ECDH alone provides no identity verification. A man-in-the-middle can intercept the public key exchange. Always add HMAC or digital signatures to authenticate the exchanged keys.
⚠️ Mismatched Curves: Both parties must use the same curve. If Alice uses P-256 and Bob uses P-384, the exchange will fail or produce different secrets.
⚠️ Reusing Ephemeral Keys: For forward secrecy, generate a fresh key pair for each session. Reusing static ECDH keys eliminates the forward secrecy benefit.
⚠️ Not Using HKDF: The shared secret should be processed through HKDF (HMAC-based Key Derivation Function) with session-specific context to bind the key to a specific purpose.

Security Notes

ℹ️ X25519 Is Recommended: X25519 is the modern default for TLS 1.3 and SSH. It is faster, has smaller keys, and avoids some implementation pitfalls of NIST curves.
ℹ️ Hybrid Key Agreement: For quantum resistance, combine X25519 with ML-KEM-768. This is exactly what TLS 1.3 deployments (X25519MLKEM768) and Signal (PQXDH) do today.
ℹ️ Forward Secrecy: Ephemeral ECDH (ECDHE) provides forward secrecy — even if a long-term key is compromised later, past sessions remain secure because each session used a fresh key pair.
ℹ️ Live Demo Included: This tool includes a live AES-256-GCM message encryption demo to verify both parties derived identical material. Type a message and both Alice and Bob encrypt/decrypt it with the shared key.

Disclaimer

This tool is provided for educational and development purposes. ECDH security depends on proper key management and authentication, which are the user's responsibility. For production systems, use well-audited TLS libraries and follow NIST SP 800-56A guidelines.

Further Reading

Related Concepts

Related Tools

Need to generate standalone ECC keys for use outside this demo? Try the ECC Key Generator. For RSA key pairs, see the RSA Key Generator. For post-quantum key encapsulation, try the ML-KEM Key Encapsulation tool.

Frequently Asked Questions

What can I do with the shared secret?

Once both parties hold the same derived key, you can use it with any symmetric encryption algorithm — AES-GCM, ChaCha20-Poly1305, etc. — to encrypt messages between them. This tool includes a live demo: type a message and both Alice and Bob encrypt it with the derived key, then decrypt each other's ciphertext, proving the full end-to-end pipeline works.

Which curve should I choose?

P-256 is the safe default — it is the standard for TLS, JWT ES256, and most libraries. P-384 and P-521 offer larger security margins for government or long-lived keys. secp256k1 is the Bitcoin/Ethereum curve, useful for blockchain interoperability. X25519 (Curve25519) is the modern minimalist choice — key agreement only, no signatures — and is the default handshake curve for TLS 1.3 and SSH. All five curves are supported and produce the same kind of shared secret when both parties use the same one.

Can I use the shared secret directly as an encryption key?

No — never use the raw shared secret directly. Hash it with SHA-256 (or SHA-384/SHA-512) first, as this tool does, or use a proper KDF like HKDF with a salt and context parameter. The raw secret has subtle statistical biases that a hash removes; a KDF additionally lets you bind the key to a specific session or purpose.

How do I import my own keys?

Paste your private key in PKCS#8 PEM or raw hex format into the Alice or Bob input field. The tool will parse it and compute the shared secret using your real key material. This lets you perform actual key exchanges with keys generated elsewhere (e.g., from OpenSSL or your application server).

What happens if I use different curves for Alice and Bob?

The key exchange will fail — both parties must use the same curve. ECDH requires that Alice and Bob operate on the same elliptic curve for the shared secret computation to produce matching results. Always verify the curve selection matches before performing the exchange.

Privacy: This tool runs entirely in your browser — nothing is uploaded or stored. · Disclaimer: For educational purposes only. Not professional security advice.

More in Key Exchange

Learn More