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.

### Working of RSA Algorithm

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

- Two prime numbers are selected as
**p**and**q** **n = pq**which is the modulus of both the keys.- Calculate
**totient = (p-1)(q-1)** - 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 - Choose
**d**such that it satisfies the equation**de = 1 + k (totient)**,**d**is the private key not known to everyone. - Cipher text is calculated using the equation
**c = m^e mod n**where**m**is the message. - 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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
//Program for RSA asymmetric cryptographic algorithm //for demonstration values are relatively small compared to practical application #include<stdio.h> #include<math.h> //to find gcd int gcd(int a, int h) { int temp; while(1) { temp = a%h; if(temp==0) return h; a = h; h = temp; } } int main() { //2 random prime numbers double p = 3; double q = 7; double n=p*q; double count; double totient = (p-1)*(q-1); //public key //e stands for encrypt double e=2; //for checking co-prime which satisfies e>1 while(e<totient){ count = gcd(e,totient); if(count==1) break; else e++; } //private key //d stands for decrypt double d; //k can be any arbitrary value double k = 2; //choosing d such that it satisfies d*e = 1 + k * totient d = (1 + (k*totient))/e; double msg = 12; double c = pow(msg,e); double m = pow(c,d); c=fmod(c,n); m=fmod(m,n); printf("Message data = %lf",msg); printf("\np = %lf",p); printf("\nq = %lf",q); printf("\nn = pq = %lf",n); printf("\ntotient = %lf",totient); printf("\ne = %lf",e); printf("\nd = %lf",d); printf("\nEncrypted data = %lf",c); printf("\nOriginal Message Sent = %lf",m); return 0; } |

## Program for RSA Algorithm in C++

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
//Program for RSA asymmetric cryptographic algorithm //for demonstration values are relatively small compared to practical application #include<iostream> #include<math.h> using namespace std; //to find gcd int gcd(int a, int h) { int temp; while(1) { temp = a%h; if(temp==0) return h; a = h; h = temp; } } int main() { //2 random prime numbers double p = 3; double q = 7; double n=p*q; double count; double totient = (p-1)*(q-1); //public key //e stands for encrypt double e=2; //for checking co-prime which satisfies e>1 while(e<totient){ count = gcd(e,totient); if(count==1) break; else e++; } //private key //d stands for decrypt double d; //k can be any arbitrary value double k = 2; //choosing d such that it satisfies d*e = 1 + k * totient d = (1 + (k*totient))/e; double msg = 12; double c = pow(msg,e); double m = pow(c,d); c=fmod(c,n); m=fmod(m,n); cout<<"Message data = "<<msg; cout<<"\n"<<"p = "<<p; cout<<"\n"<<"q = "<<q; cout<<"\n"<<"n = pq = "<<n; cout<<"\n"<<"totient = "<<totient; cout<<"\n"<<"e = "<<e; cout<<"\n"<<"d = "<<d; cout<<"\n"<<"Encrypted data = "<<c; cout<<"\n"<<"Original Message sent = "<<m; return 0; } |

**Output**

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++.

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.

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.

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

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

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

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.

Very clear and concise! Thanks!

Hey really appreciate the tutorial you have set for RSA encryption. It is very useful for people like me who is just getting started in the field. However I have a small doubt, what happens when I want to increase key length to 1024 bits (pq = 128 bytes). How would i store the key and implement mathematical functions on it since the there is not a single self sufficient variable that would be able to store this long key. I am sure I will have to take it to binary operations and use arrays, but I am not experienced as much and would really help me if you could just show me a place to start 🙂

Thanks for this beautiful piece of code.

I am trying to implement RSA and Blum Blum Shub algorithm to generate cryptographically secure pseuderandom bit stream.

Can you please explain me how to handle lagre primes in C.

I need to choose p,q such large that it will be 128 bits.

Thanking You!

Thanks for this tutorial!

Not ran the code but how can you decrypt (7) before it has been fully encrypted (6)?

Change:

double c = pow(msg,e);

double m = pow(c,d);

c=fmod(c,n);

m=fmod(m,n);

To:

double c = pow(msg,e);

c=fmod(c,n);

double m = pow(c,d);

m=fmod(m,n);

It does not work for random primes assigned to p and q. Only works for current values of p and q.

i think the issue lies in k because it’s fixed 2 to find k you need to satisfy that d and k both integers

in the relation (d*e-1)mod(tontient)=0 .. d*e+k*tontient=1 where both d and k integers solve this by doing gcd(d,tontient) and using the equations to manipulate to reach linear equation x*e+y*tontient=1 then you can use those x,y values for k and d

amazing code good job i like it