Node.js has an in-built crypto module and which can be used to run code using Javascript. This example implements the Diffie Hellman method. Each time the prime number will be fixed for the DH group selected [DH with randomly generated prime].
Node.js Diffie Hellman with fixed prime |
Theory
Bob generates a random value (\(b\)) and Alice generates a random value (\(a\)). Alice passes \(A=G^a \pmod p\) and Bob passes \(B=G^b \pmod p\). The result is then \(B^a \pmod p\) and which should be the same as \(A^b \pmod p\):
Type: modp1 Alice prime (p): ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a63a3620ffffffffffffffff Alice generator (G) : 02 Alice private key (a): 54f3e39e6757434bb6c6ebc1636fdaa628357d4edbadfefa2540cfcc8d48d916572ee01cf3cc044fdecaa3774b449b5c5d502aaea4f20aa4ed601dcbf594705fc039aec15b8e7d1469fb95ba12c9addb126902dd1c5de21b93838337703d6217 Bob private key (b): 72e9145ddae3590977dbdfdd8d4f81d4a7b6a8bdf7a6376b02e54f0e240120683e5c0ef949e4699568176d274d3e06cdf42e8ba20f2ddcd90b80750dec8c8741d9c36434a2db58eca1fa7df31991f9199913fb37d01fbb03b4d43d80f22a0066 Alice public key (A): 687ac1a91a03e11dd79251884deb93569413cc64890fb7d0b53cea37c84cc75ee00d2f9a44cb876a35e3e2745f7273759cd2d0cee9112a9af05e497381b3fd791c0e2b75b2e0fc60837c849f1a0a154da00df7bed9cb51af5953ca21c5a2d483 Bob public key (B): b8a7702e82494e1af01e9c292c5daa716df817ffe845f7bcb12d7b7a1409976d4ecc8d287654b1839515afc2e24d84cc2ecbbec69efd19456cb0e90b95986e35e9ad80d2a5bc9e03e161afd7de9e03efd7fc77b17fe572fe7d34797ca0ab4731 Alice secret: 00cbf618713fedd19945eddbcefe07de2cfd88370d3b1abbd8dff8dbe2151db3786aeef3c3f328a9db9a63e998659a78e904630ec0dbb1e078f838c6993b6cac868c6387c0b47af1de464b639948169bf709b0d4cd063947785d9e1e6f646239 Bob secret: 00cbf618713fedd19945eddbcefe07de2cfd88370d3b1abbd8dff8dbe2151db3786aeef3c3f328a9db9a63e998659a78e904630ec0dbb1e078f838c6993b6cac868c6387c0b47af1de464b639948169bf709b0d4cd063947785d9e1e6f646239
In this case the public key has 768 bits (192 hex characters). The following is some sample code
var crypto = require("crypto"); const args = process.argv.slice(3); type = args[0]; console.log("Type:\t",type); var alice = crypto.getDiffieHellman(type); var bob = crypto.getDiffieHellman(type); alice.generateKeys(); bob.generateKeys(); console.log("Alice prime (p):",alice.getPrime().toString('hex'),"\nAlice generator (G):",alice.getGenerator().toString('hex')); console.log("\nAlice private key:",alice.getPrivateKey().toString('hex')); console.log("\nBob private key:",bob.getPrivateKey().toString('hex')); console.log("\nAlice public key:",alice.getPublicKey().toString('hex')); console.log("\nBob public key:",bob.getPublicKey().toString('hex')); var alice_secret = alice.computeSecret(bob.getPublicKey(), null, 'hex'); var bob_secret = bob.computeSecret(alice.getPublicKey(), null, 'hex'); console.log("\nAlice secret:",alice_secret); console.log("\nBob secret:",bob_secret);