RSA Algorithm in C and C++ (Encryption and Decryption)

Here you will learn about RSA algorithm in C and C++.

RSA Algorithm is used to encrypt and decrypt data in modern computer systems and other electronic devices. RSA algorithm is an asymmetric cryptographic algorithm as it creates 2 different keys for the purpose of encryption and decryption. It is public key cryptography as one of the keys involved is made public. RSA stands for Ron Rivest, Adi Shamir and Leonard Adleman who first publicly described it in 1978.

RSA makes use of prime numbers (arbitrary large numbers) to function. The public key is made available publicly (means to everyone) and only the person having the private key with them can decrypt the original message.

RSA Algorithm Block Diagram

Image Source

Working of RSA Algorithm

RSA involves use of public and private key for its operation. The keys are generated using the following steps:-

  1. Two prime numbers are selected as p and q
  2. n = pq which is the modulus of both the keys.
  3. Calculate totient = (p-1)(q-1)
  4. Choose e such that e > 1 and coprime to totient which means gcd (e, totient) must be equal to 1, e is the public key
  5. Choose d such that it satisfies the equation de = 1 + k (totient), d is the private key not known to everyone.
  6. Cipher text is calculated using the equation c = m^e mod n where m is the message.
  7. With the help of c and d we decrypt message using equation m = c^d mod n where d is the private key.

Note: If we take the two prime numbers very large it enhances security but requires implementation of Exponentiation by squaring algorithm and square and multiply algorithm for effective encryption and decryption. For simplicity the program is designed with relatively small prime numbers.

Below is the implementation of this algorithm in C and C++.

Program for RSA Algorithm in C

Program for RSA Algorithm in C++


RSA Algorithm in C and C++ (Encryption and Decryption)

This article is submitted by Rahul Maheshwari. You can connect with him on facebook.

Comment below if you have any queries related to above program for rsa algorithm in C and C++.

8 thoughts on “RSA Algorithm in C and C++ (Encryption and Decryption)

  1. Nicolás

    Thanks for this tutorial!
    I’m a bit confused, the code for encryption and decryption is all together. I think the “double m” is the variable where the decrypted message is stored, but it needs “pow(c,d)” and the variable “c” needs the message “msg” because of “c= pow(msg,e)”. If I am right, how can this be possible?


    In fact, the code works correctly with current values of ‘p’ e ‘q’, but if assign other values decrypt is wrong.
    I’m not understand a utility of ‘k’, too.

    1. Shibli Qureshi

      It is because if you use large values in p, q and e then the values you will get from them will be very large which cannot be stored in even long long int datatype.

  3. An Idiot

    This code does not work. Anything other than “12” will return false decryptions.

    1. Neeraj Mishra Post author

      You have to choose value of e and d in such a may that satisfies conditions mentioned in above article. Read the conditions properly.

  4. RobbieTheK

    I confirm that anything other than “12” will return false decryptions.

  5. Akhilesh Pandey

    The code is fine but here e is incremented in every iteration until the while condition is satisfied which to me doesn’t look appealing. I suggest you to randomly choose e such that ( e <(p-q)(q-1) ) and check for the condition and then increment e.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.