def get_PublicPrivateKey(phi_n,disp,line):


	'''Starting from 3 keep trying until we find a number that is co-prime with phi_n'''
	for e in range(3,phi_n):
		if disp:
			print "Trace: try e = %d" %(e)

		'''Calcuate the values of s,t and gcd of e and phi_n using Extended Eucledian algorithm'''
		(g,t,s) = gcd(e,phi_n,disp)

		'''If gcd is 1 then break we found the required e and compute respective d using the values of s and t'''
		if g == 1:
			break

	frameinfo = getframeinfo(currentframe())
	if disp:
		print 'line:',line

	'''Nomalize d so that it is lesser than phi_n and also positive'''
	d = modulo((modulo(t,phi_n)+phi_n),phi_n)
	
	if disp:
		print "d = %d\n"%d
	return (e,d)
示例#2
0
def slope(xi, yi, q1, q2, x0, y0):
    r1 = ((xi - x0)**2 + (yi - y0)**2)**0.5
    r2 = ((xi + x0)**2 + (yi + y0)**2)**0.5

    Ex = q1 * (xi - x0) / (r1**3) + q2 * (xi + x0) / (r2**3)
    Ey = q1 * (yi - y0) / (r1**3) + q2 * (yi + y0) / (r2**3)
    E = modulo(Ex, Ey)
    return Ey / E
示例#3
0
def ggT_A2(a, b):
    if a < b:
        # Swap variables
        x = b
        b = a
        a = x
    while b > 0:
        r = modulo(a, b)
        a = b
        b = r
    return a
示例#4
0
文件: encoder.py 项目: iPhaeton/rsa
    def encode(self, string):
        message = super().encode(string)

        gcd, _, _ = self.pulverizer.calculate_gcd(message, self.public_key[1])
        if gcd != 1:
            raise Exception('Incorrect key. Generate another one.')

        # encoded_message = pow(message, self.public_key[0]) % self.public_key[1]
        encoded_message = modulo(message, self.public_key[0],
                                 self.public_key[1])
        return encoded_message
def FastExponentiation(a,x,n,disp):

	template = "{0:2}|{1:3}|{2:6}|{3:4}"
	if disp:
		print ("--------------------")
		print template.format("i","xi","y","y")
		print ("--------------------")
	x = (bin(x)[2:])[::-1]
	k = len(x)-1
	y = 1
	for i in range(k,-1,-1):
		y = modulo(y*y,n)
		y1 = y
		if x[i] == '1':
			y = modulo(a*y,n)
		y2 = y
		if disp:
			print template.format(str(i),str(x[i]),str(y1),str(y2))
	if disp:
		print ("--------------------")
	return y
def GCD(a, b):
    # Return the greatest common divisor
    if a == b:
        return a
    elif a < b:
        c = a
        a = b
        b = c
    while b > 0:
        c = b
        b = modulo(a, b)
        a = c
    return a
def PrimalityTesting(a,x,disp):
	n = x+1
	x = (bin(x)[2:])[::-1]
	k = len(x)-1
	y = 1

	template = "{0:2}|{1:3}|{2:4}|{3:4}|{4:4}" # column widths: 8, 10, 15, 7, 10
	if disp:
		print ("-----------------------")
		print template.format("i", "xi", "z", "y", "y")
		print ("-----------------------")

	for i in range(k,-1,-1):
		z = y
		y = modulo(y*y,n)
		y1=y

		if ((y == 1) and (z != 1) and (z != n-1)):
			if disp:
				print "%d is not a prime because %d^2 mod %d = 1 and %d != 1 and %d != %d - 1\n" %(n,z,n,z,z,n)
			return (a,False)
		if x[i] == '1':
			y = modulo(y*a,n)
		y2=y

		if disp:
			print template.format(str(i),str(x[i]),str(z),str(y1),str(y2))
	
	if disp:
		print ("-----------------------")
	if y != 1 :
		if disp:
			print "%d is not a prime because %d^%d mod %d != 1\n"  % (n,a,n-1,n)
		return (a,False)
	else:
		if disp:
			print "%d is perhaps a prime\n"%(n)
		return (a,True)
示例#8
0
def int_root(a, n):
    # Return n-th root of a, where b needs to be an int
    int_or_float_check(a)
    int_check(n)
    if n == 0:
        raise SyntaxError("Cannot calculate zero-th root")
    if a < 0 and modulo(n, 2) == 0:
        raise SyntaxError("Cannot do even root of negative number")
    epsilon = .00001
    x_0 = divide(a, n)
    x_1 = multiply(
        divide(1, n),
        add(multiply(add(n, -1), x_0), divide(a, int_power(x_0, add(n, -1)))))
    while abs(add(x_0, -x_1)) > epsilon:
        x_0 = x_1
        x_1 = multiply(
            divide(1, n),
            add(multiply(add(n, -1), x_0), divide(a,
                                                  int_power(x_0, add(n, -1)))))
    return x_1
def gcd(a, b, disp):
    x,y, u,v = 0,1, 1,0
    count = 1
    template = "{0:2}|{1:7}|{2:7}|{3:7}|{4:7}|{5:6}|{6:6}" # column widths: 8, 10, 15, 7, 10
    if disp:
	    print ("------------------------------------------------")
	    print template.format("i","qi","r","ri+1","ri+2","si","ti")
	    print ("------------------------------------------------")
    while a != 0:
        q, r = b/a, modulo(b,a)
        if disp:
        	print template.format(str(count),str(q),str(b),str(a),str(r),str(y),str(x))
        m, n = x-u*q, y-v*q
        b,a, x,y, u,v = a,r, u,v, m,n 
        count = count+1 

    gcd = b

    if disp:
	    print template.format(str(count),"",str(b),"","",str(y),str(x))
	    print ("------------------------------------------------\n") 
	    
    return gcd, x, y
示例#10
0
 def test_values(self):
     # Test modulo for numbers
     self.assertEqual(modulo(5, 3), 2)
     self.assertEqual(modulo(3, 5), 3)
     self.assertEqual(modulo(126, 2), 0)
 def test_modulo(self):
     self.assertEquals(modulo.modulo(5, 2), 1)
     self.assertEquals(modulo.modulo(1, 0), None)
     self.assertEquals(modulo.modulo(4, 2), 0)
     self.assertEquals(modulo.modulo(0, 0), None)
def get_RandomNumberlessThan(n):
	'''Get some random number and take its modulo with n'''
	x = get_RandomNumber()
	x = modulo(int(x,2),n)
	return x
import multi
import sub

print(
    "Select operation :\n1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5. Modulo\n"
)

while True:
    choice = input("Enter choice(1/2/3/4/5): ")

    if choice in ('1', '2', '3', '4', '5'):
        num1 = float(input("Enter first number: "))
        num2 = float(input("Enter second number: "))
        if choice == '1':
            print(num1, "+", num2, "=", addition.add(num1, num2))

        elif choice == '2':
            print(num1, "-", num2, "=", sub.substract(num1, num2))

        elif choice == '3':
            print(num1, "*", num2, "=", multi.multiply(num1, num2))

        elif choice == '4':
            print(num1, "/", num2, "=", division.divide(num1, num2))

        elif choice == '5':
            print(num1, "%", num2, "=", modulo.modulo(num1, num2))
        break
    else:
        print("Invalid Input")
示例#14
0
 def decode(encoded_message):
     # decoded_message = pow(encoded_message, private_key[0], private_key[1])
     decoded_message = modulo(encoded_message, private_key[0],
                              private_key[1])
     return alphabet_decoder.decode(decoded_message)