Skip to content

Instantly share code, notes, and snippets.

@charsi
Last active January 15, 2018 08:36
Show Gist options
  • Save charsi/eb4895999f68a79ccb68dd6428bca415 to your computer and use it in GitHub Desktop.
Save charsi/eb4895999f68a79ccb68dd6428bca415 to your computer and use it in GitHub Desktop.
// Code describing how Diffie-Hellman key exchange works. Suprisingly simple to understand.
// This does not work because Javascript can't deal with large numbers.
let g = 5;
let n = 37;
let pvtA = 5;
let pvtB = 8;
checkDiffieHellman(pvtA,pvtB,g,n);
function checkDiffieHellman (a,b,g,n){
var pubA = Math.pow(g,a)%n;
var pubB = Math.pow(g,b)%n;
var secretA = Math.pow(pubA,b)%n;
var secretB = Math.pow(pubB,a)%n;
console.log(secretA,secretB);
if (secretA === secretB){
console.log('yep this worked');
} else {
console.log('javascript ran into js-infinity',' or 9007199254740991' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment