summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBarry Mead <barrymead@cox.net>2010-02-16 07:39:49 -0700
committerBarry Mead <barrymead@cox.net>2010-02-16 07:39:49 -0700
commitae681fe8d0efb06500770797f3fdd11b3818b4f5 (patch)
treed4e8a163f6d59e73c3eacacdfd92804858e94227
parenteda00f1a24bc5ffdf39fb7dc339cf82f6a393b11 (diff)
downloadrsa-ae681fe8d0efb06500770797f3fdd11b3818b4f5.tar.gz
extended_euclid_gcd never returns negative values now
-rw-r--r--rsa/__init__.py6
1 files changed, 4 insertions, 2 deletions
diff --git a/rsa/__init__.py b/rsa/__init__.py
index a5cbb00..b45ec49 100644
--- a/rsa/__init__.py
+++ b/rsa/__init__.py
@@ -328,11 +328,13 @@ def extended_euclid_gcd(a, b):
y = 1
lx = 1
ly = 0
+ lb = b #Remember modulus (to remove negs)
while b != 0:
q = long(a/b)
(a, b) = (b, a % b)
(x, lx) = ((lx - (q * x)),x)
(y, ly) = ((ly - (q * y)),y)
+ if (lx < 0): lx += lb #No Negative return values
return (a, lx, ly)
# Main function: calculate encryption and decryption keys
@@ -353,7 +355,7 @@ def calculate_keys(p, q, nbits):
if not d == 1:
raise Exception("e (%d) and phi_n (%d) are not relatively prime" % (e, phi_n))
- if (i < 0): i += phi_n
+ if (i < 0): raise Exception("New gcd shouldn't return negative values")
if not (e * i) % phi_n == 1:
raise Exception("e (%d) and i (%d) are not mult. inv. modulo phi_n (%d)" % (e, i, phi_n))
@@ -387,7 +389,7 @@ def encrypt_int(message, ekey, n):
"""Encrypts a message using encryption key 'ekey', working modulo n"""
if type(message) is types.IntType:
- return encrypt_int(long(message), ekey, n)
+ message = long(message)
if not type(message) is types.LongType:
raise TypeError("You must pass a long or int")