Beispiel #1
0
def hack_RSA(e, n):
    '''
    Finds d knowing (e,n)
    applying the Wiener continued fraction attack
    '''
    frac = ContinuedFractions.rational_to_contfrac(
        e, n)  # Создаём list частных непрерывных дрообей [a0, ..., an]
    convergents = ContinuedFractions.convergents_from_contfrac(
        frac)  # вычисление подходящих дробей

    for (k, d) in convergents:
        # print('k, d: ', k, d)

        #check if d is actually the key
        if k != 0 and (e * d - 1) % k == 0:
            phi = (e * d - 1) // k
            s = n - phi + 1
            # check if the equation x^2 - s*x + n = 0
            # has integer roots
            discr = s * s - 4 * n
            # print(phi, s, discr)
            if (discr >= 0):
                t = Arithmetic.is_perfect_square(discr)
                # print('sqrt: ', t)
                if t != -1 and (s + t) % 2 == 0:
                    print("Hacked!")
                    return d
Beispiel #2
0
def hack_RSA(e, n):
    '''
  
  Finds d knowing (e,n)
  applying the Wiener continued fraction attack
  
  '''

    # n = input("Enter e number: ")
    # e = input("Enter n number: ")

    frac = ContinuedFractions.rational_to_contfrac(e, n)
    convergents = ContinuedFractions.convergents_from_contfrac(frac)

    for (k, d) in convergents:

        #check if d is actually the key
        if k != 0 and (e * d - 1) % k == 0:
            phi = (e * d + 1) // k
            s = n - phi + 1
            # check if the equation x^2 - s*x + n = 0
            # has integer roots
            discr = s * s - 4 * n
            if (discr >= 0):
                t = Arithmetic.is_perfect_square(discr)
                if t != -1 and (s + t) % 2 == 0:
                    print("Hacked!")

    print("-------------------------")
    print("d = ", d)
    print("-------------------------")
Beispiel #3
0
def hack_RSA(e, n):
    '''
    Finds d knowing (e,n)
    applying the Wiener continued fraction attack
    '''
    frac = ContinuedFractions.rational_to_contfrac(e, n)
    convergents = ContinuedFractions.convergents_from_contfrac(frac)

    for (k, d) in convergents:

        #check if d is actually the key
        if k != 0 and (e * d - 1) % k == 0:
            phi = (e * d - 1) // k
            s = n - phi + 1
            # check if the equation x^2 - s*x + n = 0
            # has integer roots
            '''discr = s * s - 4 * n
            if(discr >= 0):
                t = Arithmetic.is_perfect_square(discr)
                if t != -1 and (s + t) % 2 == 0:
                    print("Hacked!")
                    return d'''
            flag = 0
            for i in range(40):
                a = random.randint(2, n)
                if pow(a, phi, n) != 1:
                    flag = 1
                    break
            if flag == 0:
                print("Hacked!")
                return d
Beispiel #4
0
def wiener(e, n):
    time.sleep(1)
    frac = ContinuedFractions.rational_to_contfrac(e, n)
    convergents = ContinuedFractions.convergents_from_contfrac(frac)

    for (k, d) in convergents:
        if k != 0 and (e * d - 1) % k == 0:
            phi = (e * d - 1) // k
            s = n - phi + 1
            discr = s * s - 4 * n
            if (discr >= 0):
                t = Arithmetic.is_perfect_square(discr)
                if t != -1 and (s + t) % 2 == 0:
                    return d
Beispiel #5
0
def wiener(e,n):
    time.sleep(1)
    frac = ContinuedFractions.rational_to_contfrac(e, n)
    convergents = ContinuedFractions.convergents_from_contfrac(frac)
    
    for (k,d) in convergents:
        if k!=0 and (e*d-1)%k == 0:
            phi = (e*d-1)//k
            s = n - phi + 1
            discr = s*s - 4*n
            if(discr>=0):
                t = Arithmetic.is_perfect_square(discr)
                if t!=-1 and (s+t)%2==0:
                    return d
Beispiel #6
0
def wiener(e, n):
    frac = ContinuedFractions.rational_to_contfrac(e, n)
    convergents = ContinuedFractions.convergents_from_contfrac(frac)

    for (k,d) in convergents:
        if k!=0 and (e*d-1)%k == 0:
            phi = (e*d-1)//k
            s = n - phi + 1
            # check if the equation x^2 - s*x + n = 0
            # has integer roots
            discr = s*s - 4*n
            if(discr>=0):
                t = Arithmetic.is_perfect_square(discr)
                if t!=-1 and (s+t)%2==0:
                    return d
Beispiel #7
0
def hack_RSA(e, n):
    print "Hacking"
    frac = ContinuedFractions.rational_to_contfrac(e, n)
    convergents = ContinuedFractions.convergents_from_contfrac(frac)
    for (k, d) in convergents:
        if k != 0 and (e * d - 1) % k == 0:
            phi = (e * d - 1) // k
            s = n - phi + 1
            discr = s * s - 4 * n
            if(discr >= 0):
                t = Arithmetic.is_perfect_square(discr)
                if t != -1 and (s + t) % 2 == 0:
                    print "done"
                    print d
                    return d
Beispiel #8
0
def wiener_hack(e, n):
    # https://github.com/pablocelayes/rsa-wiener-attack
    frac = ContinuedFractions.rational_to_contfrac(e, n)
    convergents = ContinuedFractions.convergents_from_contfrac(frac)
    for (k, d) in convergents:
        if k != 0 and (e * d - 1) % k == 0:
            phi = (e * d - 1) // k
            s = n - phi + 1
            discr = s * s - 4 * n
            if (discr >= 0):
                t = Arithmetic.is_perfect_square(discr)
                if t != -1 and (s + t) % 2 == 0:
                    print("Hacked!")
                    return d
    return False
Beispiel #9
0
def hack_RSA(e, n):
    print "Hacking"
    frac = ContinuedFractions.rational_to_contfrac(e, n)
    convergents = ContinuedFractions.convergents_from_contfrac(frac)
    for (k, d) in convergents:
        if k != 0 and (e * d - 1) % k == 0:
            phi = (e * d - 1) // k
            s = n - phi + 1
            discr = s * s - 4 * n
            if (discr >= 0):
                t = Arithmetic.is_perfect_square(discr)
                if t != -1 and (s + t) % 2 == 0:
                    print "done"
                    print d
                    return d
Beispiel #10
0
def hack_RSA(e,n):
    print "[+] Wiener attack in progress..."
    frac = ContinuedFractions.rational_to_contfrac(e, n)
    convergents = ContinuedFractions.convergents_from_contfrac(frac)
    for (k,d) in convergents:
        #check if d is actually the key
        if k!=0 and (e*d-1)%k == 0:
            phi = (e*d-1)//k
            s = n - phi + 1
            # check if the equation x^2 - s*x + n = 0
            # has integer roots
            discr = s*s - 4*n
            if(discr>=0):
                t = Arithmetic.is_perfect_square(discr)
                if t!=-1 and (s+t)%2==0:
                    return d
Beispiel #11
0
def hack_RSA(e, n):
    frac = ContinuedFractions.rational_to_contfrac(e, n)
    convergents = ContinuedFractions.convergents_from_contfrac(frac)

    for (k, d) in convergents:

        #check if d is actually the key
        if k != 0 and (e * d - 1) % k == 0:
            phi = (e * d - 1) // k
            s = n - phi + 1
            # check if the equation x^2 - s*x + n = 0

            discr = s * s - 4 * n
            if (discr >= 0):
                t = Arithmetic.is_perfect_square(discr)
                if t != -1 and (s + t) % 2 == 0:
                    print("Xong!")
                    return d
Beispiel #12
0
def wiener_attack(n, e):
	'''
	Finds d knowing (e,n)
	applying the Wiener continued fraction attack
	'''
	frac = ContinuedFractions.rational_to_contfrac(e, n)
	convergents = ContinuedFractions.convergents_from_contfrac(frac)

	for (k,d) in convergents:
		#check if d is actually the key
		if k!=0 and (e*d-1)%k == 0:
			phi = (e*d-1)//k
			s = n - phi + 1
			# check if the equation x^2 - s*x + n = 0
			# has integer roots
			discr = s*s - 4*n
			if(discr>=0):
				t = helper.is_perfect_square(discr)
				if t!=-1 and (s+t)%2==0:
					print("Hacked!")
					return d
def hack_RSA(e, n):
    """
    Finds d knowing (e, n) applying the Wiener continued fraction attack
    """

    frac = ContinuedFractions.rational_to_contfrac(e, n)
    convergents = ContinuedFractions.convergents_from_contfrac(frac)

    for (k, d) in convergents:
        # Check if d is actually the key
        if k != 0 and (e * d - 1) % k == 0:
            phi = (e * d - 1) // k
            s = n - phi + 1

            # Check if the equation x^2 - s*x + n = 0 has integer roots
            discr = s * s - 4 * n
            if(discr >= 0):
                t = Arithmetic.is_perfect_square(discr)
                if t != -1 and (s + t) % 2 == 0:
                    return d
    return None
Beispiel #14
0
import RSA
import ContinuedFractions as CF
from Functions import *

keySet = RSA.RSA(
    1024, True
)  #Generates a 1024-bits weak key, verifying the conditions to apply Wiener's theorem
print('RSA key set generated')

N, e = keySet.getPublicKey()

fraction = CF.ContinuedFraction(
    e, N)  #Generates the continued fraction expantion of e/n
convergents = fraction.getConvergents()

crack_d = 0
for k, d in convergents:
    if k != 0 and (e * d) % k == 1:
        #We still need to check wether d is the right key
        phiN = (e * d -
                1) // k  #If d is the right key, this should be equal to Phi(N)
        s = N - phiN + 1  #... and this should be equal to p+q
        # We solve the equation x²-sx+N = 0, the solutions should be p and q
        disc = s * s - 4 * N
        p = (s + Sqrt(disc) +
             1) // 2  #Sqrt computes the rounded down square root
        q = (s - Sqrt(disc)) // 2
        if (p * q == N):
            crack_d = d
            break