C++ 1.) Implement the following functions for your RSA: (a) Mapping function fro
ID: 3672755 • Letter: C
Question
C++
1.) Implement the following functions for your RSA:
(a) Mapping function from alphanumerical characters to decimal digits. Every two alphanumerical characters will be converted to a block used in RSA encryption and decryption.
(b) Key generation function that takes int the inputs of two prime numbers, p and q, and generate the parameters (n, (n), e, d), and the keys for RSA. The following sub-routines may be implemented:
GCD function to find e: gcd((n), e) = 1; 1 < e < (n). Since there could be many such e values, you may use gcd to find the first five such e values.
Multiplicative inverse: d = e1(mod (n)). You may use extended Euclidean algorithm to find the first five such d values corresponding to each of five e values (Note: Your multiplicative inverse value should be within [0, (n))).
(c) Encryption function that takes each block of plaintext and public key.
(d) Decryption function that takes each block of ciphertext and private key.
(e) Mapping function from decimal digits back to alphanumerical characters.
2.) Experiment the correctness of your program by using the following plain text and prime
numbers, p = 73 and q = 151:
(a) plain text: How are you?
(b) plain text: Public key cryptography.
For each test of your RSA algorithm, you need to output the following information:
(a) RSA key information: n, (n), the first five e, and d values
(b) Original plain text
(c) Five sets of:
• Public key
• Ciphertext
• Private key
• Plaintext
(d) Decrypted plaintext from decimal digits back to alphanumerical characters.
RSA mapping below
Explanation / Answer
#include<iostream>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
long int p, q, n, t, flag, e[100], d[100], temp[100], j, m[100], en[100], i;
char msg[100];
int prime(long int);
void ce();
long int cd(long int);
void encrypt();
void decrypt();
int prime(long int pr)
{
int i;
j = sqrt(pr);
for (i = 2; i <= j; i++)
{
if (pr % i == 0)
return 0;
}
return 1;
}
int main()
{
cout << " ENTER FIRST PRIME NUMBER ";
cin >> p;
flag = prime(p);
if (flag == 0)
{
cout << " WRONG INPUT ";
exit(1);
}
cout << " ENTER ANOTHER PRIME NUMBER ";
cin >> q;
flag = prime(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++)
cout << e[i] << " " << d[i] << " ";
encrypt();
decrypt();
return 0;
}
void ce()
{
int k;
k = 0;
for (i = 2; i < t; i++)
{
if (t % i == 0)
continue;
flag = prime(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 encrypt()
{
long int pt, ct, key = e[0], 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 IS ";
for (i = 0; en[i] != -1; i++)
printf("%c", en[i]);
}
void decrypt()
{
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 IS ";
for (i = 0; m[i] != -1; i++)
printf("%c", m[i]);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.