Untitled

 avatar
unknown
plain_text
4 months ago
3.6 kB
3
Indexable
<!DOCTYPE html>
<html>
<body>
    <h1>Diffie-Hellman Key Exchange Mechanism</h1>
        <p>Exp 2: Implement the Diffie-Hellman Key Exchange mechanism using HTML and 
                JavaScript. Consider the end user as one of the parties (Alice) and the JavaScript 
                        application as the other party (Bob).</p>
                        </body>
                        <script>
                            function power(base, exp, mod) {
                                        let result = 1;
                                                base = base % mod;
                                                        while (exp > 0) {
                                                                        if (exp % 2 === 1) { // If exponent is odd, multiply base with result
                                                                                        result = (result * base) % mod;
                                                                                                    }
                                                                                                                exp = Math.floor(exp / 2); // Divide exponent by 2
                                                                                                                            base = (base * base) % mod; // Square the base
                                                        }
                                                                return result;
                            }

                                var P, G, Alice_private, Bob_private, Alice_x, Bob_y, Ka, Kb;

                                    // Publicly chosen prime numbers
                                        P = 23;
                                            G = 9;
                                                document.write("The value of first prime number P: " + P + "<br>");
                                                    document.write("The value of second prime number G: " + G + "<br>");

                                                        // Private keys chosen by Alice and Bob
                                                            Alice_private = 4;
                                                                Bob_private = 3;

                                                                    // Compute public values
                                                                        Alice_x = power(G, Alice_private, P);
                                                                            Bob_y = power(G, Bob_private, P);

                                                                                // Compute shared secret keys
                                                                                    Ka = power(Bob_y, Alice_private, P);
                                                                                        Kb = power(Alice_x, Bob_private, P);

                                                                                            document.write("The value of Alice's Secret key Ka: " + Ka + "<br><br>");
                                                                                                document.write("The value of Bob's Secret key Kb: " + Kb + "<br>");
                                                                                                </script>
                                                                                                </html>
                                                        }
                            }
Editor is loading...
Leave a Comment