How to solve “Introduction to Cryptography 1”

Whoever thought of Caeser-Encryption, ROT13 or Vigenere after seeing a crypto-challenge is stuck in the wrong Millennium -
modern cryptography mainly uses algebraic methods.
One of the most popular cryptosystems is for sure RSA. It is an asymmetric cipher: A person owns a public key and a private key. The public key can be given away to everybody and allows for the encryption of arbitrary messages.
However, only the private key enables a person to decrypt messages encrypted with the public key.

Usually, it is impossible to decrypt messages or computing the private key with only knowledge of the public key. However, this is exactly what this challenge asks us to do!

The challenge

We are given a public key (pubkey.pem) and a message (intercepted-message.txt) that was encrypted with this key. Our goal is to decrypt the intercepted message. While this is usually impossible, we will see some conditions where the goal can be achieved.

The RSA cryptosystem

The RSA cryptosystem is based on number theory. A detailed explanation of all mathematics behind RSA can be found for example on Wikipedia.

However, if we have a look at our given public key pubkey.pem, no numbers are visible. We can use OpenSSL to extract the numbers for us.

$ openssl rsa -pubin -in pubkey.pem -text -noout
RSA Public-Key: (2047 bit)
Exponent: 65537 (0x10001)

This gives us the numbers we need to work with.

The attack

For now, we have extracted the parameters of the public key. Now, there are several attacks possible. To find out which attack is successful against this key, we have to run it.
Many attacks against RSA cryptosystems are listed in this whitepaper.

However, most of the times the attack that has to be used is hinted. In our case, we know that the prime generation may be flawed. Therefore, we can try factoring the public key:

We first have to convert the modulus N of our public key to decimal, which results in

Now, we can try factoring this integer. There are many methods for factoring available, but we may just use trial division for the beginning

The following python script implements trial division and outputs a factor.

N = 10327849034940138613515485956077213322791085874638285662823764630659653931824178919168344401508423966366637831067655701114352106747323628144645384205073278784870804834942988268503504130770762781798270763453272421050209487483563600870343875197428105079394315585993355808937811229959083289653056248770988647762812998870912510238393368777882358059256678052653963583286245796285737035786447522814310717433588049686223718247661713594680120785280795132759253149754143640871380226770164628599577669124463514838464342769690232097283333816896581904763736283142031118073027496197756777460403007359764250621763279762041468943079

for i in range(2, 1000000):
    if N % i == 0:
        print("Factor found: " + str(i))

Now, knowing a factor p it is easy to compute the other factor q. From p and q on it should be easy to compute the RSA private key - but this is now left as an exercise to the reader.

After we have computed the RSA decryption exponent d, we can now proceed with decrypting our intercepted message.

d = 7312XXXXXXX
m = 4522827319495133992180681297469132393090864882907734433792485591515487678316653190385712678072377419115291918844825910187405830252000250630794128768175509500175722681252259065645121664124102118609133000959307902964132117526575091336372330412274759536808500083138400040526445476933659309071594237016007983559466411644234655789758508607982884717875864305554594254277210539612940978371460389860098821834289907662354612012313188685915852705277220725621370680631005616548237038578956187747135229995137050892471079696577563496115023198511735672164367020373784482829942657366126399823845155446354953052034645278225359074399

decrypted = pow(m, d, N)

However, we can clearly see that our result is just another number. To convert this number to ASCII, we can use the long_to_bytes function from the pycryptodome package.

from Crypto.Util.number import long_to_bytes

This code should print our flag!

Some useful tips