Let's say secret message is 18, you send 31 in public network after encryption then you decrypt using secret key 233. You get back the original message 18.
Last active
August 5, 2020 02:02
-
-
Save kiichi/db2c6c79a3f7e67702c18727eee329ce to your computer and use it in GitHub Desktop.
Find private key from pair of 2 prime numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Let's say, pick 2 prime number p = 23, q = 13 | |
// lcm(p, q) = 299 | |
var phi = 264; // N = (p-1)(q-1) is 264 | |
var e = 17; // pick prime number for the public key, GCD(e, phi(N)) has to 1 ... no common divisor | |
// what is private key d to decrypt? | |
// find combination of d and k. k could be whatever but satisfy | |
// e * d = 1 + phi(N) * k | |
// | |
for (var d=0 ;d<phi; d++) { | |
var val = (e * d - 1) / phi; | |
console.log('trying d = ',d,' checking the k - is this integer?',val, (val%1 === 0) ? 'BINGO!!!!!!!!':'nope'); | |
} | |
// example: | |
// trying d = 231 checking the k - is this integer? 14.871212121212121 nope | |
// trying d = 232 checking the k - is this integer? 14.93560606060606 nope | |
// trying d = 233 checking the k - is this integer? 15 BINGO!!!!!!!! | |
// | |
// looks like d is 233 while k is 15 | |
// how to encrypt the message? | |
// enc(m) = m^e mod N | |
// dec(enc(m)) = enc(m)^d mod N | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment