示例#1
0
def testLagrange(n):
    """Tests for each element x of Z*_n if x^phi(n) is 1"""
    order = numbthy.eulerphi(n)
    result = True
    for i in range(1,n):
        inverse = (i**order) % n
        if inverse != 1:
            print('Test failed for ' + str(i) + ': ' + str(inverse))
            result = False
    return result
示例#2
0
def root(a, e, n):
    """Efficiently find the modular root: find x where x^e = a mod n"""
    d = multiplicativeInverse(e, numbthy.eulerphi(n))
    return (a ** d) % n
示例#3
0
if (len(sys.argv) != 5):
    print "Usage: {0} p q e m".format(sys.argv[0])
    sys.exit(1) 

(p,q,e,m) = sys.argv[1:] 

#convert to int
p = int(p)
q = int(q)
e = int(e)
m = int(m)

n = p * q

#compute d using extended euclidean algorithm
phi = numbthy.eulerphi(n)
results = []
results = euclid(e,phi)
d = results[1]
if (d < 0):
    d = d + phi


print "Plaintext input: {0}".format(m)
#encryption of plaintext m into C
C = m**e % n
print "Encryption Result:{0}".format(C)


#decryption of ciphertext C back into M
M = C**d % n
示例#4
0
from numbthy import eulerphi


def are_permutations(n1, n2):
    return sorted(str(n1)) == sorted(str(n2))


limit = 10000000

ratio = 100
winning_n = 0

for n in xrange(2, limit):
    phi = eulerphi(n)
    if are_permutations(n, phi) and float(n) / phi < ratio:
        print n, phi
        winning_n = n
        ratio = float(n) / phi


print winning_n, ratio

# print sorted(str(54321))
示例#5
0
import numbthy


max_ratio = 0
max_n = 0
limit = 1000000
for x in range(1, limit):
    ratio = float(x) / numbthy.eulerphi(x)
    if ratio > max_ratio:
        print x, ratio
        max_ratio = ratio
        max_n = x

print max_ratio, max_n