Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C/C++ program to implement RSA trapdoor permutation. Your program should

ID: 3819980 • Letter: W

Question

Write a C/C++ program to implement RSA trapdoor permutation. Your program should do the following

Write a C/C + + program to implement RSA trapdoor permutation. Your program should do the following a. Implement a routine/function to generate RSA key pair(public key and secret key). You are free to use any method for generating the keys b. Implement the functionality for RSA and RSA inverse operations c. For a given plain text input, your program should execute the RSA operation with public key and output the result d. Given the output of RSA operation(from step-c above), your program should execute the RSA inverse operation with the secret key and output the original plain text.

Explanation / Answer

C++ Program for RSA Algorithm

#include<iostream>

#include<math.h>

#include<stdio.h>

#include <stdlib.h>


using namespace std;

class RSA
{

long long int n;

long long int p;

long long int q;

long long int m;

long long int phi;

long long int e;

long long int d;

public:


void generatePandQ();

bool isPrime(long long int &);

void display();

void generateEandD();

long long int gcd(long long int , long long int );

void extEuclidean(long long int , long long int , long long int &, long long int &);

long long int encryption(long long int );

long long int decryption(long long int );

};

void RSA::display()

{

cout << "n = " << n << endl;

cout << "p = " << p <<endl;

cout << "q = " << q << endl;

cout << "public key is {e,n}= " << e<<" "<<n<< endl;

cout << "phi = " << phi << endl;

cout << "private key is {d,n}= "<<d<<" "<<n<< endl;

}

void RSA::generatePandQ()

{


cout<<"enter prime number"<<endl;

cin>>p;

cout<<"second prime number"<<endl;

cin>>q;

if(isPrime(p)&&isPrime(q)&&(p!=q))

{

cout<<"continue"<<endl;

n = p * q;


phi = ( p-1 )*( q-1);


}


else

{

cout<<"wrong output"<<endl;

generatePandQ();
  

}

}

void RSA::generateEandD()

{

long long int start=1;

long long int limit=phi;

long long int t,a;


cout << "start is " << start << endl;

cout << "limit is " << limit << endl;

for(;start< limit;start++)


{
t=limit-start+1;

a = rand()%t+start;


if((gcd( phi,a) == 1))

{

e =a;

break;

}

}

long long int x = 0;

long long int y = 0;

extEuclidean(phi, e, x, y);


while(y < 0)

{

y += phi;

}

d = y;

}

void RSA::extEuclidean(long long int a,long long int b, long long int &lastx, long long int &lasty)

{

if( b == 0)

{

lastx = 1;

lasty = 0;

}

else

{

long long int quotient = a / b;

long long int remainder = a % b;

long long int s = 0;

long long int t = 0;

extEuclidean(b, remainder, s, t);

lastx = t;

lasty = s - quotient * t;

}

}

long long int RSA::gcd( long long int a, long long int b)

{

long long int temp = 0;

while(b != 0)

{

temp = a;

a = b;

b = temp % b;

}

return a;

  
}

bool RSA::isPrime( long long int &x)

{

if(x==2)
return true;

for( long long int i = 2; i<x;i++)

if( x % i == 0)
return false;

else
return true;


}

long long int RSA::encryption( long long int msg)

{

long long int i = e;

long long int e_msg = 1;

while(i)

{

i--;

e_msg = (e_msg * msg) % n;


}

return e_msg % n;


}

long long int RSA::decryption(long long int e_msg)

{

long long int i = d;

long long int d_msg = 1;

while(i)

{

i--;

d_msg = (d_msg * e_msg) % n;

}

return d_msg % n;

}

int main()

{

clock_t start = clock();


long long int message;

RSA obj;

obj.generatePandQ();


cout << "Enter the Message : ";

cin >> message;

obj.generateEandD();

obj.display();

long long int e_msg = 0;


e_msg = obj.encryption(message);


cout << "Encrypted message is = " << e_msg << endl;

long long int d_msg = 0;

d_msg = obj.decryption(e_msg);

cout << "Decrypted message is = " << d_msg << endl;

return 0;


}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote