ECDH Key Exchange Online | Elliptic Curve Diffie-Hellman Browser
Key Exchange 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
| Curve | Key Size | Security Level | RSA Equivalent | Best for |
|---|---|---|---|---|
| P-256 | 256-bit | 128-bit | ~3072-bit | TLS, JWT, general-purpose |
| P-384 | 384-bit | 192-bit | ~7680-bit | Government, payment |
| P-521 | 521-bit | 256-bit | ~15360-bit | Maximum security |
| secp256k1 | 256-bit | 128-bit | ~3072-bit | Bitcoin, Ethereum |
| X25519 | 256-bit | 128-bit | ~3072-bit | TLS 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
Security Notes
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
- RFC 7748 — Elliptic Curves for Security (X25519, X448)
- RFC 8446 — TLS 1.3 Key Exchange
- NIST SP 800-56A — Key Agreement Using Discrete Logarithm Cryptography
- Signal Protocol — Double Ratchet Algorithm
Related Concepts
- ECC Key Generator — Generate standalone ECC keys for use outside this demo
- ML-KEM Key Encapsulation — Post-quantum key agreement (FIPS 203)
- AES Encrypt/Decrypt — Use the derived shared secret for symmetric encryption
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
RSA Key Generator
Generate RSA key pairs online in PEM, DER, JWK, SSH, or PKCS#1 format. Batch generation, passphrase encryption, key import.
ECC / ECDSA Key Generator
Generate ECC key pairs on P-256, P-384, P-521, and secp256k1 online. Export PEM/JWK/hex, sign and verify ECDSA, derive ECDH secrets.
ML-KEM Key Encapsulation
Generate ML-KEM (FIPS 203) post-quantum key pairs online. Encapsulation and decapsulation with ML-KEM-512/768/1024, hex and Base64 output.