Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jvdsn
GitHub Repository: jvdsn/crypto-attacks
Path: blob/master/attacks/rsa/common_modulus.py
2589 views
1
from sage.all import ZZ
2
from sage.all import xgcd
3
4
5
def attack(n, e1, c1, e2, c2):
6
"""
7
Recovers the plaintext from two ciphertexts, encrypted using the same modulus and different public exponents.
8
:param n: the common modulus
9
:param e1: the first public exponent
10
:param c1: the ciphertext of the first encryption
11
:param e2: the second public exponent
12
:param c2: the ciphertext of the second encryption
13
:return: the plaintext
14
"""
15
g, u, v = xgcd(e1, e2)
16
p1 = pow(c1, u, n) if u > 0 else pow(pow(c1, -1, n), -u, n)
17
p2 = pow(c2, v, n) if v > 0 else pow(pow(c2, -1, n), -v, n)
18
return int(ZZ(int(p1 * p2) % n).nth_root(g))
19
20