Code Breakers
Time limit1sMemory limit128 MB
Factor each RSA modulus up to 10^9, recover the private exponent, and decrypt the given ciphertext.
- Level
Medium5 of 10
- Topics
- Number theory, Math, Brute force
- Solved
- No attempts yet
Problem
It is almost certain that the NSA intercepts and logs, and probably analyzes, much of the e-mail traffic in the world, and certainly in the US. One way organizations and individuals protect the privacy of their messages is encryption, and one of the cooler ideas in encryption is public key encryption. Roughly, it goes like this. You publish one key, so everyone can see it and use it to encrypt a message to you. Decrypting that message takes a different key, which only you know. The idea sounds nice, and the far bigger contribution was showing that it is actually mathematically possible, which is what the RSA algorithm did.
Here is how it works. You choose two prime numbers and (usually at random, though that does not matter for this problem). Your public key, which everyone can see, consists of the number and a small number . Your private key, which only you should know, is a number such that . If you know and , finding such a is easy. If you do not, it seems difficult, and that is why only you, the owner of and , can decrypt the message.
Now, how to encrypt and decrypt a message. Say the message is a number (any string becomes a number by reading its bits as one). The encrypted version is , which anyone can compute from and alone. To decrypt, the recipient computes , which recovers the original message.
To break someone's key, it is enough to factor and recover and . This is believed, though not proved, to be hard as long as and are large enough (thousands of digits). If they are too small, brute force breaks RSA. That is what you will do here.
The R, S and A in RSA come from the surnames Rivest, Shamir and Adleman.
Input
The first line contains the number of data sets. Each of the next lines holds one data set as three integers , , separated by spaces, with , and . In other words, the attacker knows the public key and the encrypted message. In these inputs is always a product of exactly two primes, and is always given so that a satisfying the condition above exists.
Output
For each data set, print Data Set x: on a line by itself, where is its number. Then print the decrypted message of the ciphertext on a line by itself, followed by an empty line.