Skip to content

Instantly share code, notes, and snippets.

@sim642
Created August 14, 2013 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sim642/6230624 to your computer and use it in GitHub Desktop.
Save sim642/6230624 to your computer and use it in GitHub Desktop.
from fractions import gcd
from random import randint
def egcd(a, b):
if a == 0:
return (b, 0, 1)
else:
g, y, x = egcd(b % a, a)
return (g, x - (b // a) * y, y)
def modinv(a, m):
g, x, y = egcd(a, m)
if g != 1:
raise Exception('modular inverse does not exist')
else:
return x % m
(p, q) = (61, 53) #get the primes however you want
n = p * q
tot = (p - 1) * (q - 1)
e = randint(2, tot - 1)
while gcd(e, tot) != 1:
e = randint(2, tot - 1)
d = modinv(e, tot)
print (n, e, d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment