gist: Buggy RSA implementation, 2014-01-26 16:32:01Z
#!/usr/bin/env/python
import math
import random
def test_prime(p):
max_test = int(math.sqrt(p))+1
for x in range(2,max_test):
if p%x == 0:
return False
return True
def gcd(x,y):
z=y%x
if not z == 0:
return gcd(z,x)
else:
return x
max_primes = 100
min_primes = 50
primes = []
for x in range(min_primes,max_primes):
if test_prime(x):
primes.append(x)
p = random.choice(primes)
q = random.choice(primes)
n=p*q
phi = (p-1)*(q-1)
possible_e_s = []
for x in range(1,phi):
if gcd(x,phi) == 1:
possible_e_s.append(x)
e = random.choice(possible_e_s)
for x in range(phi):
if (e*x)%phi == 1:
d = x
print (n,e)
del phi
del p
del q
messages = list(range(30))
m = random.choice(messages)
c = pow(m,e,n)
print c
message = pow(c,d,n)
if m == message:
a = ''
a += "\n ____ ____ _ __ __ _ _ \n| _ \\"
a += " / ___| / \\ \\ \\ / /__ _ __| | _____| |\n| |_) | \\__"
a += "_ \\ / _ \\ \\ \\ /\\ / / _ \\| '__| |/ / __| |\n| _ < _ ___) "
a += "| / ___ \\ _ \\ V V / (_) | | | <\\__ \\_|\n|_| \\_(_)____(_)_/"
a += " \\_(_) \\_/\\_/ \\___/|_| |_|\\_\\___(_) "
a += " \n"
a += '-'*100
print a
else:
print "`m` was:\t", m
print "`message`:\t", message