Created
October 6, 2022 17:02
-
-
Save memochou1993/34ff2cfa8c8dc15fc88c3db75495a913 to your computer and use it in GitHub Desktop.
Revisions
-
memochou1993 created this gist
Oct 6, 2022 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,44 @@ const gcd = (a, b) => b ? gcd(b, a % b) : a const p = 61n const q = 53n const n = p * q const r = (p - 1n) * (q - 1n) let e = 1n for (let i = 2n; i < r; i++) { if (gcd(i, r) === 1n) { e = i break } } let d for (let i = e + 1n; i < r; i++) { if ((i * e) % r === 1n) { d = i break } } const pubKeyN = n const pubKeyE = e const priKeyN = n const priKeyD = d const m = 100n const encrypted = m ** pubKeyE % pubKeyN const decrypted = encrypted ** priKeyD % n console.log('p', p) console.log('q', q) console.log('r', r) console.log('pubKeyN', pubKeyN) console.log('pubKeyE', pubKeyE) console.log('priKeyN', priKeyN) console.log('priKeyD', priKeyD) console.log('m', m) console.log('encrypted', encrypted) console.log('decrypted', decrypted)