There’s a great paper I found by Dan Boneh from 1998 highlighting the weaknesses of the RSA cryptosystem. I found this paper to be a particularly enlightening read (and interestingly enough, it’s been 20 years since that paper!), so here I’m going to reiterate some of the attacks described in the paper, but using examples with numbers in them.

That being said, I am going to skip over the primer of how the RSA cryptosystem works, since there’s already a great number of resources on that.

Factoring large integers

Obviously this is a pretty bruteforce-ish way to crack the cryptosystem, and probably won’t work in time for you to see the result, but can still be considered an attack vector. This trick works by just factoring the modulus, NN. With NN, finding the private exponent dd from the public exponent ee is a piece of cake.

Let’s choose some small numbers to demonstrate this one (you can follow along in a Python REPL if you want):

>>> N = 881653369
>>> e = 17
>>> c = 875978376

NN is clearly factorable in this case, and we can use resources like msieve or factordb to find smaller primes in this case. Since we know now that N=20717×42557N = 20717 \times 42557, we can find the totient of NN:

>>> p = 20717
>>> q = 42557
>>> tot = (p - 1) * (q - 1)

Now all that’s left is to discover the private exponent and solve for the original message! (you can find the modular inverse function I used here)

>>> d = modinv(e, tot)
>>> pow(c, d, N)

And that’s it! Now let’s look at some more sophisticated attacks…

Elementary attacks

These attacks are related to the misuse of the RSA system. (if you can’t tell, I’m mirroring the document structure of the original paper)

Common modulus

My cryptography professor gave this example as well. Suppose there was a setup in which the modulus was reused, maybe for convenience (although I suppose with libraries today, it’d actually be more inconvenient to reuse the key). Key pairs would be issued to different users and they would share public keys with each other and keep private keys to themselves.

The problem here is if you have a key pair, and you got someone else’s public key, you could easily derive the private key by just factoring the modulus. Let’s see how this works with a real example now.

Since this is a big problem if you were to really use this cryptosystem, I’ll be using actual keys from an actual crypto library instead of the small numbers like in the first example to show that this works on 2048-bit RSA. The library is called PyCrypto, and if you’re planning on doing anything related to crypto with Python, it’s a good tool to have with you. For now, I’m going to generate a 2048-bit key (by the way, in practice you probably shouldn’t be using 2048-bit keys anymore, I’m just trying to spare my computer here).

>>> from Crypto.PublicKey import RSA
>>> k1 = RSA.generate(2048)
<_RSAobj @0x7f3d3226dfd0 n(2048),e,d,p,q,u,private>

Now, normally when you generate a new key, it’d generate a new modulus. For the sake of this common modulus attack, we’ll force the new key to use the same modulus. This also means we’ll have to choose an exponent ee other than the default choice of 65537 (see this link for documentation):

>>> N = k1.p * k1.q
>>> e = k1.e
>>> d = k1.d
>>> e2 = 65539
>>> d2 = modinv(e2, (k1.p - 1) * (k1.q - 1))
>>> k2 = RSA.construct((N, e2, d2))
<_RSAobj @0x7f3d31c7c5f8 n(2048),e,d,p,q,u,private>

Ok, now we have two keys, k1k_1 and k2k_2. Now I’ll show how using only the public and private key of k1k_1 (assuming this is the pair that we got legitimately from the crypto operator), and the public key of k2k_2, which is tied to the same modulus, we can find the private key of k2k_2.

To do this, we’ll try to find the roots of the equation:

f(x)=x2(p+q)x+pqf(x) = x^2 - (p + q)x + pq

You’ll find that for values of pp and qq, this will produce f(p)=p2p2\-qp+pqf(p) = p^2 - p^2 \- qp + pq, and f(q)=q2pqq2+pqf(q) = q^2 - pq - q^2 + pq. We know that N=pqN = pq. How can we find p+qp + q? Since ϕ(N)=(p1)(q1)=pqpq+1\phi(N) = (p - 1)(q - 1) = pq - p - q + 1, we can find that ϕ(N)=N(p+q)+1\phi(N) = N - (p + q) + 1, so p+q=Nϕ(N)+1p + q = N - \phi(N) + 1.

Now we need to use ee and dd to estimate ϕ(N)\phi(N). Recall that ed=1modϕ(N)ed = 1 \mod \phi(N). This is equivalent to saying ed=1+kϕ(N)ed = 1 + k\phi(N). Then ed1ϕ(N)=k\frac{ed - 1}{\phi(N)} = k.

It turns out that kk is extremely close to edN\frac{ed}{N}:

edN=1+kϕ(N)N=1N+kϕ(N)N\frac{ed}{N} = \frac{1 + k\phi(N)}{N} = \frac{1}{N} + \frac{k\phi(N)}{N}

1N\frac{1}{N} is basically 0, and ϕ(N)\phi(N) is very close to NN, so it shouldn’t change the value of kk by very much. We now use edN\frac{ed}{N} to estimate kk:

ϕ(N)=ed1edN\phi(N) = \frac{ed - 1}{\frac{ed}{N}}

>>> from decimal import Decimal, getcontext
>>> getcontext().prec = 1000
>>> k = round(Decimal(e) * Decimal(d) / Decimal(N))
>>> phi = (Decimal(e) * Decimal(d) - 1) / Decimal(k)

Then we can get p+qp + q through the formula mentioend above:

>>> B = Decimal(N) - phi + 1
>>> C = Decimal(N)

Check to make sure BB and CC are integers. If they’re not, try using a higher precision in getcontext().prec. Now solve the quadratic equation:

>>> p = (B + (B * B - 4 * C).sqrt()) / Decimal(2)
>>> q = (B - (B * B - 4 * C).sqrt()) / Decimal(2)
>>> p * q == N

We’ve successfully recovered pp and qq from just NN, ee, and dd!


This attack is actually about RSA signatures (which uses the opposite keys as encryption: private for signing and public for verifying), and shows how you can compute the signature of a message MM using the signature of a derived message MM'.

Suppose Marvin wants Bob to sign the following message: "I (Bob) owes Marvin $100,000 USD". Marvin hands this to Bob saying something like, “I’ll just need you to sign this with your private key.” Let’s generate Bob’s private key:

>>> from Crypto.Util.number import bytes_to_long, long_to_bytes
>>> from Crypto.PublicKey import RSA
>>> bob = RSA.generate(2048)
<_RSAobj @0x7f4309521128 n(2048),e,d,p,q,u,private>
>>> M = b"I (Bob) owes Marvin $100,000 USD"

Obviously, Bob, an intellectual, will refuse to sign the message. However, suppose Marvin now transforms his message into a more innocent looking one. He does this by turning MM into M=reMmodNM' = r^eM \mod N where r is an integer that’s coprime to NN:

>>> from random import randint
>>> N = bob.p * bob.q # this is publicly available knowledge
>>> r = 19
>>> Mp = long_to_bytes((pow(r, bob.e, N) * bytes_to_long(M)) % N)

Now he asks Bob to sign this more… innocently-looking message. Without questioning, Bob, an intellectual, signs his life away. Let’s say he produces a signature

S=(Md) =(reM)d =redMd =rMdmodN\begin{aligned} S' &= (M'^d) \\\ &= (r^e * M)^d \\\ &= r^{ed} * M^d \\\ &= r * M^d \mod N \end{aligned}
>>> Sp, = bob.sign(Mp, 0)

Now, all Marvin has to do is multiply by the modular inverse of rr, to obtain MdM^d, the signature of the original message:

>>> S = (Sp * modinv(r, N)) % N

Sure enough, if you try to verify the “original” signature against the original message, it checks out.

>>> bob.verify(M, (S,))

Marvin has now successfully tricked Bob into signing his life away.

This post is a work in progress.. I’ll update it as I add more.