Skip to content

Instantly share code, notes, and snippets.

@RichardEllicott
Last active May 1, 2018 12:42
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 RichardEllicott/250cd0ac180d8c95ca666b6919419c0e to your computer and use it in GitHub Desktop.
Save RichardEllicott/250cd0ac180d8c95ca666b6919419c0e to your computer and use it in GitHub Desktop.
calculate n,e,d vars from p and q to create an RSA key
def calculate_rsa_nedpq(p=1090660992520643446103273789680343, q=1162435056374824133712043309728653):
"""
input large primes p and q, returns the 5 components in a tuple (n, e, d, p, q) which can be used to construct private keys like:
RSA.construct()
https://crypto.stackexchange.com/questions/19444/rsa-given-q-p-and-e?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
"""
def egcd(a, b):
x, y, u, v = 0, 1, 1, 0
while a != 0:
q, r = b // a, b % a
m, n = x - u * q, y - v * q
b, a, x, y, u, v = a, r, u, v, m, n
gcd = b
return gcd, x, y
e = 65537 # the same number on generated keys to
# compute n
n = p * q
# Compute phi(n)
phi = (p - 1) * (q - 1)
# Compute modular inverse of e
gcd, a, b = egcd(e, phi)
d = a
return n, e, d, p, q
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment