This repository has been archived by the owner on May 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsolve.py
107 lines (95 loc) · 2.19 KB
/
solve.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/python
import pwnlib.tubes as pwntube
import pwnlib.util.fiddling as bitfn
from struct import *
import sys, time
from sympy.ntheory.modular import crt
from decimal import *
print 'RSA'
import sys
sys.setrecursionlimit(1500)
ns=[]
cs=[]
with open('output') as myfile :
for i in xrange(17):
n=myfile.readline()[3:-1]
e=myfile.readline()[3:-1]
c=myfile.readline()[3:-1]
if e == '7':
ns.append(n)
cs.append(c)
ns=map(int,ns)
cs=map(int,cs)
from sympy import integer_nthroot
def isprintable(s, codec='utf8'):
try: s.decode(codec)
except UnicodeDecodeError:
return False
else:
return True
def int_nthroot(n, r): # returns (rounded root, whether root is int)
(root, exact) = integer_nthroot(n,r)
if exact:
return root
else:
return None
def CRT(ds, rs):
'''
Chinese Remainder Theorem
ds: array of dividers
rs: array of remainders
Return the number s such that s mod ds[i] = rs[i]
'''
length = len(ds)
if not length == len(rs):
print "The lengths of the two must be the same"
return None
p = i = prod = 1
s = 0
for i in range(length):
prod *= ds[i]
for i in range(length):
p = prod // ds[i]
s += rs[i] * modInv(p, ds[i]) * p
return s % prod
def modInv(a, m):
'''
Return r such that a*r mod m = 1
'''
g, x, y = eGCD(a, m)
if g != 1:
print("no inverse")
exit()
return None
else:
return x % m
def eGCD(a, b):
'''
Extended Euclidean gcd. Return g,x,y such that ax+by=g=gcd(a,b)
'''
if a == 0:
return (b, 0, 1)
else:
g, y, x = eGCD(b%a, a)
return (g, x-(b//a)*y, y)
def execute(keys, cs):
'''
ns: array of n
e: public exponent
cs: array of cipher text
'''
ns = []
for key in keys:
ns.append(key.n)
e = keys[0].e
return decrypt(ns,cs,e)
def decrypt(ns,cs,e):
s =CRT(ns, cs)
pt =int_nthroot(s, e)
if pt is not None:
return pt
else:
print "Cannot find %dth root of %s" % (e, hex(s))
return None
aa=decrypt(ns,cs,7)
print format(aa,'x').decode('hex')