Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jvdsn
GitHub Repository: jvdsn/crypto-attacks
Path: blob/master/attacks/rsa/hastad_attack.py
2589 views
1
import os
2
import sys
3
from math import gcd
4
5
path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(os.path.abspath(__file__)))))
6
if sys.path[1] != path:
7
sys.path.insert(1, path)
8
9
from attacks.rsa import low_exponent
10
from shared.crt import fast_crt
11
12
13
def attack(N, e, c):
14
"""
15
Recovers the plaintext from e ciphertexts, encrypted using different moduli and the same public exponent.
16
:param N: the moduli
17
:param e: the public exponent
18
:param c: the ciphertexts
19
:return: the plaintext
20
"""
21
assert e == len(N) == len(c), "The amount of ciphertexts should be equal to e."
22
23
for i in range(len(N)):
24
for j in range(len(N)):
25
if i != j and gcd(N[i], N[j]) != 1:
26
raise ValueError(f"Modulus {i} and {j} share factors, Hastad's attack is impossible.")
27
28
c, _ = fast_crt(c, N)
29
return low_exponent.attack(e, c)
30
31