Write a C/C++ program to implement RSA trapdoor permutation. Your program should
ID: 3827628 • Letter: W
Question
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. You have to submit: 1) The program source code 2) output of key generation routine/function to show the two keys generated 3) Example plain text input and the output of RSA operation execution with public key 4) The execution output of RSA inverse operation when run with the output from step 3.Explanation / Answer
Code to Implement RSA trapdoor Permutations as per specifications are as follows :
#include<iostream>
#include<math.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
long int p;
long int q, n, t;
long int flag, e[100], d[100];
long int temp[100], j;
long int m[100], en[100], i;
char msg[100];
int primeFunction(long int);
void ce();
long int cd(long int);
void encryptFunction();
void decryptFunction();
int primeFunction(long int pr)
{
int i;
j = sqrt(pr);
for (i = 2; i <= j; i++) // For loop for prime function
{
if (pr % i == 0)
return 0;
}
return 1;
}
int main()
{
cout << " FIRST PRIME NUMBER :";
cin >> p;
flag = primeFunction(p);
if (flag == 0)
{
cout << " WRONG INPUT ";
exit(1);
}
cout << " ANOTHER PRIME NUMBER : ";
cin >> q;
flag = primeFunction(q);
if (flag == 0 || p == q)
{
cout << " WRONG INPUT ";
exit(1);
}
cout << " ENTER MESSAGE : ";
fflush(stdin);
cin >> msg;
for (i = 0; msg[i] != NULL; i++)
m[i] = msg[i];
n = p * q;
t = (p - 1) * (q - 1);
ce();
cout << " POSSIBLE VALUES OF e AND d ARE ";
for (i = 0 ; i < j - 1 ; i++) // for loop in main function
cout << e[i] << " " << d[i] << " ";
encryptFunction();
decryptFunction();
return 0;
}
void ce()
{
int k;
k = 0;
for (i = 2; i < t; i++)
{
if (t % i == 0)
continue;
flag = primeFunction(i);
if (flag == 1 && i != p && i != q)
{
e[k] = i;
flag = cd(e[k]);
if (flag > 0)
{
d[k] = flag;
k++;
}
if (k == 99)
break;
}
}
}
long int cd(long int x)
{
long int k = 1;
while (1)
{
k = k + t;
if (k % x == 0)
return (k / x);
}
}
void encryptFunction()
{
long int pt;
long int ct, key = e[0];
long int k, len;
i = 0;
len = strlen(msg);
while (i != len)
{
pt = m[i];
pt = pt - 96;
k = 1;
for (j = 0; j < key; j++)
{
k = k * pt;
k = k % n;
}
temp[i] = k;
ct = k + 96;
en[i] = ct;
i++;
}
en[i] = -1;
cout << " THE ENCRYPTED MESSAGE : ";
for (i = 0; en[i] != -1; i++)
printf("%c", en[i]);
}
void decryptFunction()
{
long int pt, ct, key = d[0], k;
i = 0;
while (en[i] != -1)
{
ct = temp[i];
k = 1;
for (j = 0; j < key; j++)
{
k = k * ct;
k = k % n;
}
pt = k + 96;
m[i] = pt;
i++;
}
m[i] = -1;
cout << " THE DECRYPTED MESSAGE : ";
for (i = 0; m[i] != -1; i++)
printf("%c", m[i]);
}
Output :
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.