import { createEffect, createSignal } from "solid-js"; export function Variable({ variableName, id, textClass = "text-gray-500 sm:text-sm", ...properties }) { return (
); } function Complete() { const [alicePrivateKey, setAlicePrivateKey] = createSignal(null); const [bobPrivateKey, setBobPrivateKey] = createSignal(null); const [g, setG] = createSignal(5); // Usually 2000 or 4000 bits long const [n, setN] = createSignal(23); const alicePublicKey = () => alicePrivateKey() ? Math.pow(g(), alicePrivateKey()) % n() : null; const bobPublicKey = () => bobPrivateKey() ? Math.pow(g(), bobPrivateKey()) % n() : null; // const [bobPublicKey, setBobPublicKey] = createSignal(null); const sharedSecret = () => { if (!bobPublicKey()) { return null; } return Math.pow(bobPublicKey(), alicePrivateKey()) % n(); }; let alicePublicKeyInput; let bobPublicKeyInput; createEffect(() => { if (!alicePublicKeyInput || !alicePublicKey()) { return; } alicePublicKeyInput.value = alicePublicKey(); }); createEffect(() => { if (!bobPublicKeyInput || !bobPublicKey()) { return; } bobPublicKeyInput.value = bobPublicKey(); }); // Hiding public key for simplicity but make it optionally visible later const isPublicKeyHidden = true; function handlePrivateKeyInput(setPrivateKey) { return (event) => { if (event.target.value > n()) { console.debug("Private key has to be smaller than n"); return; } if (event.target.value < 1) { console.debug("Private key has to be bigger than 0"); return; } setPrivateKey(event.target.value); }; } // TODO add public and private area return (

Diffie Hellman Demo

Alice

Public

Bob

1. Decide on a Public Key

Public Key: (g = {g()}, n = {n()}){" "}

{!isPublicKeyHidden && (
Public Key setG(event.target.value * 1)} /> setN(event.target.value * 1)} />
)}

2. Decide on a private key

A number between 1 and{" "} n = {n()}

3. Compute the public key

Alice' Public Key
A ={" "} g a mod{" "} n

{/* "A" just for spacing */} A = {g()} {alicePrivateKey() || "a"} {" "} mod {n()}

{/* "A" just for spacing */} A ={" "} {alicePublicKey() ?? "?"} {" "} {!alicePublicKey() && ( {" "} (Please enter a private key{" "} a) )}

Bob's Public Key
B ={" "} g b mod{" "} n

{/* "A" just for spacing */} B = {g()} {bobPrivateKey() || "b"} {" "} mod {n()}

{/* "A" just for spacing */} B ={" "} {bobPublicKey() ?? "?"}{" "} {!bobPublicKey() && ( {" "} (Please enter a private key{" "} b) )}

4. Exchange the public keys

5. Compute the shared secret

Shared Secret
s ={" "} B a mod{" "} n

{/* "A" just for spacing */} B ={" "} {bobPublicKey() || "B"} {alicePrivateKey() || "a"} {" "} mod {n()}

{/* "A" just for spacing */} B ={" "} {function () { if (!alicePrivateKey()) return ( {" "} (Please enter Alice's private key{" "} a) ); if (!bobPublicKey()) return ( {" "} (Please enter Bob's public key{" "} B) ); return ( {sharedSecret()} ); }}

Shared Secret
s ={" "} A b mod{" "} n

{/* "A" just for spacing */} A ={" "} {alicePublicKey() || "A"} {bobPrivateKey() || "b"} {" "} mod {n()}

{/* "A" just for spacing */} B ={" "} {function () { if (!bobPrivateKey()) return ( {" "} (Please enter Bob's private key{" "} b) ); if (!alicePublicKey()) return ( {" "} (Please enter Alice's public key{" "} B) ); return ( {sharedSecret()} ); }}

); } export default Complete;