Exemple #1
0
def chinese_rem(x, m1, m2):
    a1 = c_mod(x, m1) + m1
    a2 = c_mod(x, m2) + m2
    M = m1 * m2
    M1 = M / m1
    M2 = M / m2
    y1 = gmpy2.invert(M1, m1)
    y2 = gmpy2.invert(M2, m2)
    return (a1 * M1 * y1) + (a2 * M2 * y2)
Exemple #2
0
def ex(c, d, N):
    i = 1
    m = 1

    # compute c^d mod N
    while i <= d:
        if i & d != 0:
            m = gmpy2.c_mod(m * c, N) + N
        i = i << 1
        c = gmpy2.c_mod(c * c, N) + N
    return m
Exemple #3
0
def get_prime_factor(euler_value):
    """求素因子"""
    prime_factor = []
    for i in range(1, m):
        if gmpy2.c_mod(euler_value,i) == 0 and gmpy2.is_prime(i):
            prime_factor.append(i)
    return prime_factor
def create_gauss_matrix(V, list_primes):
    A = [[]]
    for i in range(0, len(V)):
        num = V[i]
        vector = []
        for p in range(0, n_smooth + 1):
            value = 0
            while gmpy2.c_mod(num, list_primes[p]) == 0:
                num = gmpy2.div(num, list_primes[p])
                value ^= 1
            vector.append(value)
        A.append(vector)

    print("cac so co the tao tich binh phuong: " + str(len(V)))
    print(V[:20])
    return A[1:]
Exemple #5
0
def rsa_mitm(n, e, c, b):
    S = random.randint(pow(2,b)/2 , pow(2,b))
    T = random.randint(pow(2,b)/2 , pow(2,b))
    n=gmpy2.mpz(n)
    cs=[gmpy2.mpz()]*(S)
    print S
    print T
    print n

    for s in range(1,S):
        cs[s] = gmpy2.mpz( gmpy2.c_mod(gmpy2.div(c,( gmpy2.powmod(s,e,n))) , n) )
        #print (s,cs[s])
    S_ = (T)/100
    for t in range(1,T):
        for s in range(1,S):
            if cs[s] == (gmpy2.powmod(t,e,n)):
                print (s,s*t)
                return s*t
    print cs
    return "Plaintext nicht gefunden"
Exemple #6
0
def rsa_(n,e,c,b):
    T = pow(2,b)
    n=gmpy2.mpz(n)
    c=gmpy2.mpz(c)
    cs=[gmpy2.mpz()]*(T)
    # counter = 0
    # T_ = T/100
    # percent= 1
    for r in range(1,T):
        cs[r] = gmpy2.c_mod(gmpy2.div(c,gmpy2.powmod(r,e,n)),n)
        # counter += 1
        # if (counter >= T_):
        #     print percent
        #     percent +=1
        #     counter = 0
    #cs.sort()
    print cs
    for s in range (1, T):
        try:
            index=cs.index(gmpy2.powmod(s,e,n))
            #print (gmpy2.c_mod(index*s,n),cs[index],gmpy2.powmod(s,e,n) )
            return index*s
        except ValueError:
Exemple #7
0
import sys
sys.path.append('/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/')
import numbthy
import gmpy2
N1 = 179769313486231590772930519078902473361797697894230657273430081157732675805505620686985379449212982959585501387537164015710139858647833778606925583497541085196591615128057575940752635007475935288710823649949940771895617054361149474865046711015101563940680527540071584560878577663743040086340742855278549092581
N2 = 648455842808071669662824265346772278726343720706976263060439070378797308618081116462714015276061417569195587321840254520655424906719892428844841839353281972988531310511738648965962582821502504990264452100885281673303711142296421027840289307657458645233683357077834689715838646088239640236866252211790085787877
N3 = 720062263747350425279564435525583738338084451473999841826653057981916355690188337790423408664187663938485175264994017897083524079135686877441155132015188279331812309091996246361896836573643119174094961348524639707885238799396839230364676670221627018353299443241192173812729276147530748597302192751375739387929
# challenge 1

gmpy2.get_context().precision = 1000
A1 = gmpy2.isqrt(N1) + 1

x1 = gmpy2.isqrt(A1 * A1 - N1)
p1,q1 = A1 - x1, A1 + x1

assert gmpy2.c_mod(N1,p1) == 0
print p1
#challenge 2

A2 = gmpy2.isqrt(N2) + 1
for i in range(2**20+1):
	x2 = gmpy2.isqrt(A2 * A2 - N2)
	if gmpy2.c_mod(N2,A2 - x2) == 0:
		print A2 - x2
		break
	A2 += 1
else:
	print "None found"

# challenge 3
Exemple #8
0
            m = gmpy2.c_mod(m * c, N) + N
        i = i << 1
        c = gmpy2.c_mod(c * c, N) + N
    return m

p = 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084171

g = 11717829880366207009516117596335367088558084999998952205599979459063929499736583746670572176471460312928594829675428279466566527115212748467589894601965568

h = 3239475104050450443565264378728065788649097520952449527834792452971981976143292558073856937958553180532878928001494706097394108577585732452307673444020333

B = 1 << 20
table = dict()
gx1 = 1
for x1 in xrange(0, B):
    temp = gmpy2.c_mod(gmpy2.invert(gx1, p) * h, p) + p
    table[temp] = x1
    gx1 = gmpy2.c_mod(gx1 * g, p) + p

print len(table)

res = 1
for x0 in xrange(0, B):
    # print res
    if res in table:
        x1 = table[res]
        print x1
        print x0
        print x0 * B + x1
        break
    res = gmpy2.c_mod(res * gx1, p) + p
Exemple #9
0
fi = (p - 1) * (q - 1)

e = 65537

d = gmpy2.invert(e, fi)

i = 1
m = 1
c = 22096451867410381776306561134883418017410069787892831071731839143676135600120538004282329650473509424343946219751512256465839967942889460764542040581564748988013734864120452325229320176487916666402997509188729971690526083222067771600019329260870009579993724077458967773697817571267229951148662959627934791540
N = 179769313486231590772930519078902473361797697894230657273430081157732675805505620686985379449212982959585501387537164015710139858647833778606925583497541085196591615128057575940752635007475935288710823649949940771895617054361149474865046711015101563940680527540071584560878577663743040086340742855278549092581

# compute c^d mod N
while i <= d:
    if i & d != 0:
        m = gmpy2.c_mod(m * c, N) + N
    i = i << 1
    c = gmpy2.c_mod(c * c, N) + N

h = hex(m)
print h
l = len(h) - 1
while (l > 1 and not(h[l] == '0' and h[l - 1] == '0')):
    l = l - 1

print h[l+1:].decode('hex')




Exemple #10
0
import binascii

p = 13407807929942597099574024998205846127479365820592393377723561443721764030073662768891111614362326998675040546094339320838419523375986027530441562135724301
q = 13407807929942597099574024998205846127479365820592393377723561443721764030073778560980348930557750569660049234002192590823085163940025485114449475265364281

fi = (p - 1) * (q - 1)

e = 65537

d = gmpy2.invert(e, fi)

i = 1
m = 1
c = 22096451867410381776306561134883418017410069787892831071731839143676135600120538004282329650473509424343946219751512256465839967942889460764542040581564748988013734864120452325229320176487916666402997509188729971690526083222067771600019329260870009579993724077458967773697817571267229951148662959627934791540
N = 179769313486231590772930519078902473361797697894230657273430081157732675805505620686985379449212982959585501387537164015710139858647833778606925583497541085196591615128057575940752635007475935288710823649949940771895617054361149474865046711015101563940680527540071584560878577663743040086340742855278549092581

# compute c^d mod N
while i <= d:
    if i & d != 0:
        m = gmpy2.c_mod(m * c, N) + N
    i = i << 1
    c = gmpy2.c_mod(c * c, N) + N

h = hex(m)
print h
l = len(h) - 1
while (l > 1 and not (h[l] == '0' and h[l - 1] == '0')):
    l = l - 1

print h[l + 1:].decode('hex')
hash_list_eq1 = {}

B = mpz(2**20)
gB = gmpy2.powmod(g, B, p)

print 'Building Hash Table'
t0 = time.time()

# Equation h/g**x1
for x1 in xrange(1, B):
    temp = gmpy2.powmod(g, x1, p)
    temp = gmpy2.div(h, temp)
    print temp

    temp = gmpy2.c_mod(temp, p)
    hash_list_eq1[temp] = x1

print 'Finished building hash table (%s)' % (time.time() - t0)

print 'Comparing results to hash table'
t0 = time.time()

# Equation (g**B)**x0
for x0 in xrange(1, B):
    value = gmpy2.powmod(gB, x0, p)

    if value in hash_list_eq1:
        print 'FOUND! %s, %s' % (x0, hash_list_eq1[value])
        print 'RESULT = %s' % (gmpy2.c_mod(gmpy2.add(gmpy2.mul(x0, B), x1), p))
Exemple #12
0
p4 = p
q4 = q
N4 = N

#
# Challenge 2
N = 648455842808071669662824265346772278726343720706976263060439070378797308618081116462714015276061417569195587321840254520655424906719892428844841839353281972988531310511738648965962582821502504990264452100885281673303711142296421027840289307657458645233683357077834689715838646088239640236866252211790085787877

# N is a product of p and q where |p-q| < 2^11*N^(1/4)
# try A from sqrt(N) and up 
A = add((isqrt(N)), 1)
while True:
    x = isqrt(sub(mul(A,A), N))
    p = sub(A, x)
    # use mod to test!
    if (c_mod(N, p) == 0):
        print str(p) + '\n'
        break
    A = add(A, 1)

#
# Challenge 3
N = 720062263747350425279564435525583738338084451473999841826653057981916355690188337790423408664187663938485175264994017897083524079135686877441155132015188279331812309091996246361896836573643119174094961348524639707885238799396839230364676670221627018353299443241192173812729276147530748597302192751375739387929 

# N is a product of p and q where |3p + 2q| < N^(1/4)
# try A as ceil(sqrt(6N)) and x = sqrt(A^2 - 6N)

# first find A = ceil(sqrt(6N))
A = mul(6, N)
A = add(A, 1)
A = isqrt(A)
Exemple #13
0
def RSA_encrypt(x, e, N):
    xe = pow(mpz(x), e)
    cipher_Enc = c_mod(mpz(xe), N) + N
    return cipher_Enc