Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nickolaswiebe committed Apr 26, 2017
1 parent 492a714 commit 783ffe8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion math/extended_gcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 17,4 @@ def extended_gcd(a,b):
old_s, s = s, old_s - quotient * s
old_t, t = t, old_t - quotient * t

return old_s,old_t,old_r
return old_s, old_t, old_r
17 changes: 11 additions & 6 deletions math/rsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,31 45,36 @@ def modinv(a, m):

"""
the RSA key generating algorithm
k is the number of bits in n
"""
def generate_key(k):
e = genprime(k)
# size in bits of p and q need to add up to the size of n
p_size = k / 2
q_size = k - p_size

e = genprime(k) # in many cases, e is also chosen to be a small constant

while True:
p = genprime(k/2)
p = genprime(k / 2)
if p % e != 1:
break

while True:
q = genprime(k - k/2)
q = genprime(k - k / 2)
if q % e != 1:
break

n = p * q
l = (p - 1) * (q - 1)
l = (p - 1) * (q - 1) # calculate totient function
d = modinv(e,l)

return n,e,d
return n, e, d

"""
sample usage:
n,e,d = generate_key(1024)
data = 1337
encrypted = pow(data,e,n)
decrypted = pow(encrypted,d,n)
assert decrypted == encrypted
assert decrypted == data
"""

0 comments on commit 783ffe8

Please sign in to comment.