# Untitled

a guest
Aug 25th, 2019
67
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import collections
2. from Crypto.PublicKey import RSA
3. from Crypto.Util import number
4. from math import gcd
5.
6. class Data:
7. def __init__(self, idx):
8. self.idx = idx
9. self.public_key = None
10. self.private_key = None
11. self.ciphertext = None
12. self.plaintext = None
13.
14. def get_data(idx):
15. data = Data(idx)
16. with open('{}.pem'.format(idx), 'rb') as pem:
18. with open('{}.bin'.format(idx), 'rb') as bin:
20. return data
21.
22. datas = [get_data(idx) for idx in range(1, 101)]
23.
24. def make_private_key(p, q, e=65537):
25. n = p * q
26. phi = (p - 1) * (q - 1)
27. d = number.inverse(e, phi)
28. return RSA.construct((n, e, d))
29.
30. primes = {}
31. for data1 in datas:
32. for data2 in datas:
33. if data1 == data2: continue
34.
35. div = gcd(data1.public_key.n, data2.public_key.n)
36. if div == 1: continue
37.
38. p1 = data1.public_key.n // div
39. p2 = data2.public_key.n // div
40.
41. if data1.private_key is None:
42. data1.private_key = make_private_key(div, p1)
43. if data2.private_key is None:
44. data2.private_key = make_private_key(div, p2)
45.
46. for data in datas:
47. if data.private_key is not None:
48. # data.ciphertext = data.public_key.encrypt(b'hello world', 0)
49. data.plaintext = data.private_key.decrypt(data.ciphertext)
50.
51. with open('out/{}.pem'.format(data.idx), 'wb') as out:
52. out.write(data.private_key.exportKey(format='PEM'))
53. with open('out/{}.dec'.format(data.idx), 'wb') as out:
54. print('{}.dec: {}'.format(data.idx, data.plaintext))
55. out.write(data.plaintext)
RAW Paste Data