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

Here you will find out about RSA calculation in C and C++.

RSA Algorithm is utilized to scramble and decode information in current PC frameworks and other electronic gadgets.

RSA calculation is a lopsided cryptographic calculation as it makes 2 distinct keys with the end goal of encryption and decoding.

It is open key cryptography as one of the keys included is made open. RSA represents Ron Rivest,

Adi Shamir and Leonard Adleman who first openly depicted it in 1978.

RSA utilizes prime numbers (subjective enormous numbers) to work. People in general key

is made accessible openly (intends to everybody) and just the individual having the private key with them can decode the first message.

Working of RSA Algorithm

RSA includes the utilization of open and private key for its activity.

The keys are produced utilizing the accompanying advances:-

Two prime numbers are chosen as p and q

n = pq which is the modulus of both the keys.

Figure totient = (p-1)(q-1)

Pick e to such an extent that e > 1 and coprime to totient which means gcd (e, totient) must be equivalent to 1, e is people in general key

Pick d with the end goal that it fulfils the condition de = 1 + k (totient), d is the private key not known to everybody.

Figure content is determined to utilize the condition c = m^e mod n where m is the message.

With the assistance of c and d, we decode message utilizing condition m = c^d mod n where d is the private key.

Note: If we take the two prime numbers enormous it improves security however requires execution

of Exponentiation by squaring calculation and square and duplicate calculation for viable encryption and decoding.

For effortlessness, the program is planned with moderately little prime numbers.

The following is the usage of this calculation in C and C++.

Program for RSA Algorithm in C

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

//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

Remark beneath in the event that you have any inquiries identified with the above program for RSA calculation in C and C++.

Leave a Comment

error: Alert: Content is protected!!