|
| 1 | +import time |
| 2 | +import random |
| 3 | +import urllib |
| 4 | +import json |
| 5 | +import math |
| 6 | +public_url = "http://localhost:8080/publickey" |
| 7 | + |
| 8 | +response = urllib.urlopen(public_url) |
| 9 | +data = json.loads(response.read()) |
| 10 | + |
| 11 | +N = int(data['n']) |
| 12 | + |
| 13 | +print N |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +def generateZSet(): |
| 18 | + z = [random.randint(N**(1/3.0) + 1, N**(1/2.0) - 1) for p in range(10000)] |
| 19 | + m_list = [] |
| 20 | + for message in z: |
| 21 | + encrypt_url = "http://localhost:8080/encrypt?message={0}".format(message) |
| 22 | + response = urllib.urlopen(encrypt_url) |
| 23 | + data = json.loads(response.read()) |
| 24 | + m_list.append(data['signature']) |
| 25 | + |
| 26 | + return m_list |
| 27 | +def generateYSet(): |
| 28 | + y = [random.randint(0, N**(1/3.0)-1) for p in range(10000)] |
| 29 | + m_list = [] |
| 30 | + |
| 31 | + for message in y: |
| 32 | + encrypt_url = "http://localhost:8080/encrypt?message={0}".format(message) |
| 33 | + response = urllib.urlopen(encrypt_url) |
| 34 | + data = json.loads(response.read()) |
| 35 | + m_list.append(data['signature']) |
| 36 | + return m_list |
| 37 | + |
| 38 | +def egcd(a, b): |
| 39 | + if a == 0: |
| 40 | + return (b, 0, 1) |
| 41 | + else: |
| 42 | + g, y, x = egcd(b % a, a) |
| 43 | + return (g, x - (b // a) * y, y) |
| 44 | + |
| 45 | +def modinv(a, m): |
| 46 | + gcd, x, y = egcd(a, m) |
| 47 | + if gcd != 1: |
| 48 | + return None # modular inverse does not exist |
| 49 | + else: |
| 50 | + return x % m |
| 51 | + |
| 52 | +def generate_r(n): |
| 53 | + x = 0 |
| 54 | + while(2**x < n): |
| 55 | + x += 1 |
| 56 | + return 2 ** x |
| 57 | + |
| 58 | +def monPro(a, b, r, n): |
| 59 | + |
| 60 | + r_inv = modinv(r, n) |
| 61 | + n_prime = (r * r_inv - 1)/n |
| 62 | + |
| 63 | + t = a * b |
| 64 | + m = (t*n_prime) % r |
| 65 | + u = (t + m*n)/r |
| 66 | + if (u >= n): |
| 67 | + return (u-n, True) |
| 68 | + return (u, False) |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | +def modExp(M, d, n, r): |
| 73 | + M_bar = (M*r) % n |
| 74 | + C_bar = r % n |
| 75 | + d_guess = d |
| 76 | + d_guess += '1' |
| 77 | + reduction = False |
| 78 | + for ei in d_guess: |
| 79 | + C_bar, reduction = monPro(C_bar, C_bar, r, n) |
| 80 | + if(ei == '1'): |
| 81 | + C_bar, reduction = monPro(M_bar, C_bar, r, n) |
| 82 | + |
| 83 | + return reduction |
| 84 | + |
| 85 | +def RSACheckReduction(guess, message, n, r): |
| 86 | + return modExp(message, guess, n, r) |
| 87 | + |
| 88 | +def sendGuess(message): |
| 89 | + decrypt_url = "http://localhost:8080/decrypt?message={0}".format(message) |
| 90 | + start = time.time() |
| 91 | + response = urllib.urlopen(decrypt_url) |
| 92 | + end = time.time() |
| 93 | + return end-start |
| 94 | + |
| 95 | + |
| 96 | +def getDecryptTime(message): |
| 97 | + l = [] |
| 98 | + for i in range(300): |
| 99 | + l.append(sendGuess(message)) |
| 100 | + return median(l) |
| 101 | + return reduce(lambda x, y: x + y, l) / len(l) |
| 102 | + |
| 103 | + |
| 104 | +def median(lst): |
| 105 | + even = (0 if len(lst) % 2 else 1) + 1 |
| 106 | + half = (len(lst) - 1) / 2 |
| 107 | + return sum(sorted(lst)[half:half + even]) / float(even) |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | +def getSampleMessages(size): |
| 113 | + return [random.randint(100000, 10000000) for s in range(size)] |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | +class RSAAttack(object): |
| 118 | + def __init__(self): |
| 119 | + self.upper = int(math.sqrt(N)) |
| 120 | + self.lower = int("1" + "0" * 15, 2) - 1 |
| 121 | + |
| 122 | + def guess(self): |
| 123 | + pUpper = 0 |
| 124 | + pLower = 0 |
| 125 | + previousUpVal = 1000 |
| 126 | + previousDownVal = 1000 |
| 127 | + for i in range(0, 8): |
| 128 | + print self.upper |
| 129 | + print self.lower |
| 130 | + up = getDecryptTime(self.upper) |
| 131 | + low = getDecryptTime(self.lower) |
| 132 | + print up |
| 133 | + print low |
| 134 | + |
| 135 | + if (up - low > 0): |
| 136 | + self.upper = int(math.ceil((self.upper + self.lower) / 2.0)) |
| 137 | + else: |
| 138 | + self.lower = int(math.floor((self.upper + self.lower) / 2.0)) |
| 139 | + previousUpVal = up |
| 140 | + |
| 141 | + print "upper: ", self.upper |
| 142 | + print "lower: ", self.lower |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | +def doAttack(n): |
| 148 | + r = generate_r(n) |
| 149 | + bits_to_solve = 16 |
| 150 | + secretKey = [1] + [0] * 15 |
| 151 | + upper = int("1111111111111111", 2) |
| 152 | + av1 = getDecryptTime(str(upper)) |
| 153 | + lower = int("1000000000000000" , 2) |
| 154 | + av0 = getDecryptTime(str(lower)) |
| 155 | + print "upper: ", av1 |
| 156 | + print "lower: ", av0 |
| 157 | + print "upper - lower: ", av1 - av0 |
| 158 | + # for x in range(1, 16): |
| 159 | + # print secretKey |
| 160 | + # av0 = getDecryptTime(str(int("".join(map(lambda x:str(x), secretKey)), 2))) |
| 161 | + # secretKey[x] = 1 |
| 162 | + # av1 = getDecryptTime(str(int("".join(map(lambda x:str(x), secretKey)), 2))) |
| 163 | + # if av1 - av0 > 0: |
| 164 | + # continue |
| 165 | + # else: |
| 166 | + # secretKey[x] = 0 |
| 167 | + # print secretKey |
| 168 | + |
| 169 | + |
| 170 | +# for i in xrange(N/4, N/2): |
| 171 | +# print bin(i) |
| 172 | + |
| 173 | +#doAttack(N) |
| 174 | + |
| 175 | +#a = RSAAttack() |
| 176 | +#a.guess() |
| 177 | +print getDecryptTime(41453) |
| 178 | +#print getDecryptTime(42314) |
0 commit comments