How RSA algorithm works

Subscribe for New Posts


Cryptography is a method of protecting information and communications through the use of codes, so that only the intended users can read and process it. With the increase in cyber crimes these days, there is a dire need of more and more secure ways to protect information. One of the successful algorithms which even prevails in today’s world and is used almost in every plethora of virtual world is RSA algorithm which is named after its creators Ron Rivest, Adi Shamir and Leonard Adleman. It is one of the most secure algorithms and it is believed that only the quantum computers can break this algorithm in a feasible way.

In this blog post, I will discuss the working of this algorithm and how you can use this understanding in order to solve the CTF problems which are based on RSA encryption and decryption.

How RSA works

Let us understand this with the help of simple example and a communication medium and then we will try to break down each part of it in detail.

Let us assume Bob wants to send the plain text(P) B to Alice.

Above are the steps how RSA works. Now, it’s the time to unravel the mysteries behind the selection of values for e,N and d.

Math behind the RSA Algorithm

Let us see step by step how the math behind the algorithm works.

CTF problems based on RSA algorithm

I have already discussed about CTF’s in my very first post. In the problems relating to RSA decryption, you’ll be given a text file which contains e, N and C. Your task is to find the flag by decrypting the cipher text C.

For instance we are given;

Now, Let us use our understanding in order to figure out the solution. We need to calculate p and q in order to calulate Φ(N). There is an online platform which is very handy in figuring out primes p and q for us. Put N in there and factorize it in order to get p and q.

Factor db result
Factor db result

Now, that we have p, q, e, n and C. Let us code it out to get d and our plain text P. Python’s crypto and gympy2 libraries do the task easier for us by obfuscating the complexity. However, if you wish you can implement the algorithm using above steps from scratch.

from Crypto.PublicKey import RSA
import gmpy2

# For converting the integer result to its text form
def int2Text(number, size):
    text = "".join([chr((number >> j) & 0xff) for j in reversed(range(0, size << 3, 8))])
    return text.lstrip("\x00")

N=245841236512478852752909734912575581815967630033049838269083
C=219878849218803628752496734037301843801487889344508611639028
p=416064700201658306196320137931
q=590872612825179551336102196593
phi_n=(p-1)*(q-1)
e=3L
# Calculation of the private key
d = long(gmpy2.divm(1, e, phi_n))
# Constructing the RSA 
rsa = RSA.construct((N,e,d,p,q))
# Decrypting the cipher text using RSA object
plaintext = rsa.decrypt(C)

print plaintext  # returns 142327357830311032682897538242625561166817486779261
print int2Text(plaintext,100) #returns abctf{rs4_is_aw3s0m3}

Bingo; we captured the flag.

Conclusion

The compexity of RSA algorithm lies in the fact that the prime factorization of large number is hard. This is the only reason why RSA is still prevelant and is used in every area across virtual world. However, RSA algorithm is not impenetrable. Quantum computers can crack it very efficiently. You can watch this amazing youtube video on further elaboration.