-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchinese_attack.py
37 lines (32 loc) · 1.14 KB
/
chinese_attack.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
#! /usr/bin/env python3
import gmpy2
import binascii
class chinese_attack():
def __init__(self, attackobjs):
self.ln = [1]*len(attackobjs)
self.len = len(attackobjs)
self.ntot = 1
self.m = 0
self.test = False
self.attackobjs = attackobjs
self.e = attackobjs[0].pub_key.e
def system_solve(self):
""" This function find the solution of the congruency system """
for i in range(self.len):
self.ntot *= self.attackobjs[i].pub_key.n
for j in range(self.len):
if i != j:
self.ln[i] *= self.attackobjs[j].pub_key.n
for i in range(self.len):
self.ln[i] *= gmpy2.invert(self.ln[i], self.attackobjs[i].pub_key.n)
for i in range(self.len):
self.m += self.attackobjs[i].cipherdec * self.ln[i]
self.m = self.m % self.ntot
self.m, self.test = gmpy2.iroot(self.m, self.e)
if not self.test:
print("Miscalculation")
exit()
def print_value(self):
self.m=str(hex(self.m))[2:] #long
self.m=binascii.unhexlify(self.m)
return self.m