Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ashutosh1206
GitHub Repository: ashutosh1206/crypton
Path: blob/master/RSA-encryption/Attack-Retrieve-Modulus/extractmod.py
1402 views
1
from Crypto.Util.number import *
2
3
4
def extractmod_eknown(_encrypt, e, limit=4):
5
"""
6
Reference: https://crypto.stackexchange.com/questions/43583/deduce-modulus-n-from-public-exponent-and-encrypted-data
7
8
Function to extract the value of modulus, given value of public key exponent
9
10
:input parameters:
11
_encrypt : <type 'function'> : Function interacting with the server for encryption
12
e : <type 'int' or 'long'> : Public Key exponent
13
limit : <type 'int'> : number of values to be sent for encryption
14
"""
15
try:
16
assert limit <= 4
17
except AssertionError:
18
print "[+] Limit too big!"
19
return -1
20
try:
21
m_list = [2, 3, 5, 7]
22
mod_list = [(bytes_to_long(_encrypt(long_to_bytes(m_list[i])))) - (m_list[i]**e) for i in range(limit)]
23
_GCD = mod_list[0]
24
for i in range(limit):
25
_GCD = GCD(_GCD, mod_list[i])
26
return _GCD
27
except Exception as ex:
28
print "[+] Exception: ", ex
29
30
def extractmod_eunknown(_encrypt, limit=4):
31
"""
32
Reference: https://crypto.stackexchange.com/questions/43583/deduce-modulus-n-from-public-exponent-and-encrypted-data
33
34
Function to extract the value of modulus without the value of public key exponent
35
36
:input parameters:
37
_encrypt : <type 'function'> : Function interacting with the server for encryption
38
limit : <type 'int'> : number of values to be sent for encryption
39
"""
40
try:
41
assert limit <= 4
42
except AssertionError:
43
print "[+] Limit too big!"
44
return -1
45
try:
46
m_list = [2, 3, 5, 7]
47
ct_list = [bytes_to_long(_encrypt(long_to_bytes(m_list[i]**2))) for i in range(limit)]
48
ct_list2 = [bytes_to_long(_encrypt(long_to_bytes(m_list[i]))) for i in range(limit)]
49
assert len(ct_list) == len(ct_list2)
50
mod_list = [(ct_list2[i]**2 - ct_list[i]) for i in range(limit)]
51
_gcd = mod_list[0]
52
for i in mod_list:
53
_gcd = GCD(_gcd, i)
54
return _gcd
55
except Exception as ex:
56
print "[+] Exception: ", ex
57
return -1
58
59