minor updates
This commit is contained in:
parent
70c706f0c2
commit
aefb61cae8
21
rsatest.py
21
rsatest.py
@ -41,6 +41,8 @@ class bcolors:
|
|||||||
ENDC = '\033[0m'
|
ENDC = '\033[0m'
|
||||||
|
|
||||||
|
|
||||||
|
print "RSA Key Generation, Encryption and Decryption example\n"
|
||||||
|
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
# Generating the Public/Private keypair
|
# 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..."
|
print "1. looking for two distinct prime numbers p and q in artificially small range..."
|
||||||
i = 0
|
i = 0
|
||||||
while i < 2:
|
while i < 2:
|
||||||
rand = random.randint(100, 999)
|
rand = random.randint(0x80, 0xff)
|
||||||
if is_prime(rand):
|
if is_prime(rand):
|
||||||
if i == 1:
|
if i == 1:
|
||||||
q=rand
|
q=rand
|
||||||
@ -75,19 +77,21 @@ while i < 2:
|
|||||||
p=rand
|
p=rand
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
print "p =", bcolors.DRED, p, bcolors.ENDC, "\tprime?", is_prime(p)
|
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)
|
print "q =", bcolors.DRED, q, bcolors.ENDC, "\tprime?", is_prime(q), bcolors.YELLOW, "(prime2)", bcolors.ENDC
|
||||||
print
|
print
|
||||||
|
|
||||||
|
|
||||||
# 2. Compute n = pq.
|
# 2. Compute n = pq.
|
||||||
|
|
||||||
print "2. computing n = pq ..."
|
print "2. computing the modulus n = pq ..."
|
||||||
n = p * q
|
n = p * q
|
||||||
print "n = p * q =", bcolors.DRED, p, bcolors.ENDC, "*", bcolors.DRED, q, bcolors.ENDC, "=", \
|
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
|
||||||
|
|
||||||
|
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.
|
# 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.
|
# 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 "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 - "+ \
|
"Dan Boneh's Twenty Years of Attacks on the RSA Cryptosystem - "+ \
|
||||||
"http://crypto.stanford.edu/~dabo/pubs/papers/RSA-survey.pdf\n"
|
"http://crypto.stanford.edu/~dabo/pubs/papers/RSA-survey.pdf\n"
|
||||||
#e_gcd = 2
|
#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_gcd = gcd(e, f_n)
|
||||||
|
|
||||||
e = 65537
|
e = 65537
|
||||||
print "e =", bcolors.CYAN, e, bcolors.ENDC
|
print "e =", bcolors.CYAN, e, bcolors.ENDC, bcolors.YELLOW, "(publicExponent)", bcolors.ENDC
|
||||||
print
|
|
||||||
print
|
print
|
||||||
|
|
||||||
# 5. Determine d as d ≡ e^−1 (mod φ(n)); i.e., d is the multiplicative inverse of e (modulo φ(n)).
|
# 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)) ..."
|
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)
|
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
|
||||||
|
|
||||||
print "Public key is modulus n =", bcolors.BLUE, n, bcolors.ENDC, "and the public (or encryption) exponent e =", \
|
print "Public key is modulus n =", bcolors.BLUE, n, bcolors.ENDC, "and the public (or encryption) exponent e =", \
|
||||||
|
Loading…
Reference in New Issue
Block a user