Rick'S Algorithm (crypto)
Last updated
Last updated
For this challenge we get Python source code which is running on the server.
It implements classic RSA, with an encryption and decryption oracle. However, the decryption oracle imposes the following restrictions on the ciphertext: we cannot have c % pow(flag,e,n) == 0 or flag % pow(c,d,n) == 0
Below is my script:
Background: In normal RSA decryption, the encrypted message is , and decryption works because since d = pow(e, -1, phi)
(i.e. d
is the inverse of e
mod phi
).
The first problem is that we do not even have the value of the modulus n
. We can get it by encrypting two distinct plaintexts, say and . Then our ciphertexts will be and , which can also be expressed as and for some non-negative and . Since we know and we can then obtain and , and taking the GCD of those will most likely give us .
We can bypass the check by asking for the decryption of a value which is not c
, but the decrypted plaintext allows us to find m
easily. Once such example is . The decryption would give us . By taking the inverse of the decrypted plaintext we can get the flag. However it is worth noting that this solution may sometimes fail due to not having an inverse mod n.