minor updates

master
Andy 10 years ago
parent 70c706f0c2
commit aefb61cae8

@ -41,6 +41,8 @@ class bcolors:
ENDC = '\033[0m'
print "RSA Key Generation, Encryption and Decryption example\n"
# -----------------------------------------------------------------------------
# Generating the Public/Private keypair
# -----------------------------------------------------------------------------
@ -67,7 +69,7 @@ def is_prime(num):
print "1. looking for two distinct prime numbers p and q in artificially small range..."
i = 0
while i < 2:
rand = random.randint(100, 999)
rand = random.randint(0x80, 0xff)
if is_prime(rand):
if i == 1:
q=rand
@ -75,19 +77,21 @@ while i < 2:
p=rand
i += 1
print "p =", bcolors.DRED, p, bcolors.ENDC, "\tprime?", is_prime(p)
print "q =", bcolors.DRED, q, bcolors.ENDC, "\tprime?", is_prime(q)
print "p =", bcolors.DRED, p, bcolors.ENDC, "\tprime?", is_prime(p), bcolors.YELLOW, "(prime1)", bcolors.ENDC
print "q =", bcolors.DRED, q, bcolors.ENDC, "\tprime?", is_prime(q), bcolors.YELLOW, "(prime2)", bcolors.ENDC
print
# 2. Compute n = pq.
print "2. computing n = pq ..."
print "2. computing the modulus n = pq ..."
n = p * q
print "n = p * q =", bcolors.DRED, p, bcolors.ENDC, "*", bcolors.DRED, q, bcolors.ENDC, "=", \
bcolors.BLUE, n, bcolors.ENDC
bcolors.BLUE, n, bcolors.ENDC, bcolors.YELLOW, "(modulus)", bcolors.ENDC
print
print "Private-Key will be "+bcolors.YELLOW+str(n.bit_length())+bcolors.ENDC+" bit long\n"
# 3. Compute φ(n) = φ(p)φ(q) = (p 1)(q 1) = n - (p + q - 1), where φ is Euler's totient function.
@ -100,7 +104,7 @@ print
# 4. Choose an integer e such that 1 < e < φ(n) and gcd(e, φ(n)) = 1; i.e., e and φ(n) are coprime.
print "4. looking for an integer e such that 1 < e < φ(n) and gcd(e, φ(n)) = 1; i.e., e and φ(n) are coprime ..."
print "\nSetting e = 2^16+1 (65537) as per recommendation in\n"+ \
print "Setting e = 2^16+1 (65537) as per recommendation in\n"+ \
"Dan Boneh's Twenty Years of Attacks on the RSA Cryptosystem - "+ \
"http://crypto.stanford.edu/~dabo/pubs/papers/RSA-survey.pdf\n"
#e_gcd = 2
@ -110,8 +114,7 @@ print "\nSetting e = 2^16+1 (65537) as per recommendation in\n"+ \
# e_gcd = gcd(e, f_n)
e = 65537
print "e =", bcolors.CYAN, e, bcolors.ENDC
print
print "e =", bcolors.CYAN, e, bcolors.ENDC, bcolors.YELLOW, "(publicExponent)", bcolors.ENDC
print
# 5. Determine d as d ≡ e^1 (mod φ(n)); i.e., d is the multiplicative inverse of e (modulo φ(n)).
@ -133,7 +136,7 @@ def modinv(a, m):
print "5. Determining d as d ≡ e^1 (mod φ(n)); i.e., d is the multiplicative inverse of e (modulo φ(n)) ..."
d = modinv(e, f_n)
print "d =", bcolors.RED, d, bcolors.ENDC
print "d =", bcolors.RED, d, bcolors.ENDC, bcolors.YELLOW, "(privateExponent)", bcolors.ENDC
print
print "Public key is modulus n =", bcolors.BLUE, n, bcolors.ENDC, "and the public (or encryption) exponent e =", \

Loading…
Cancel
Save