Asymmetric encryption has been broadly used for information security in modern s
ID: 3792774 • Letter: A
Question
Asymmetric encryption has been broadly used for information security in modern society. In this project, you are asked to implement and test a prototype for RSA, an asymmetric encryption system. The program should:
1. Generate two large pseudo-prime numbers;
2.Check if those numbers by doing the pseudo-primality testing
2. Use the two numbers to form a public encryption key and the private decryption key;
3. Create a main program that allows a user to key in a message in plaintext and encrypt it with public key; and decrypt a ciphered-text to plaintext with private key.
RSA cryptosystem •
Setup:
– n = pq, with p and q primes
– e relatively prime to f(n) = (p - 1) (q - 1)
– d inverse of e in Zf(n) • Keys:
– Public key: KE = (n, e)
– Private key: KD = d • Encryption:
– Plaintext M in Zn – C = Me mod n • Decryption:
– M = Cd mod n •
Example
– Setup:
• p = 7, q = 17 • n = 717 = 119
• f(n) = 616 = 96
• e = 5
• d = 77 (5· 77 mod 96 =1)
– Keys:
• public key: (119, 5)
• private key: 77
– Encryption:
• M = 19
• C = 195 mod 119 = 66
– Decryption:
• C = 6677 mod 119 = 19
PYTHON IS THE RECOMENDED LANGUAGE BUT YOU CAN STILL FEEL FREE TO IMPLEMENT WITH OTHER LANGUAGES
Explanation / Answer
Answer:
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include<math.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
long int p1, q1, n1, t1, flag1, e1[100], d1[100], temp1[100], j, m1[100], en1[100], i;
char msg1[100];
int primechk(long int); //function to check prime number
void cei();
long int cd1(long int);
void encrypting(); //method of encryption
void decrypting(); //method of decryption
int primechk(long int prime) //prime number checking
{
int i;
j = sqrt(prime);
for (i = 2; i <= j; i++)
{
if (prime % i == 0)
return 0;
}
return 1;
}
int main()
{
cout << " Enter the first prime number ";
cin >> p1;
flag1 = primechk(p1);
if (flag1 == 0) //If number is not prime then print the wrong input
{
cout << " Wrong Input ";
exit(1);
}
cout << " Enter the second prime number ";
cin >> q1;
flag1 = primechk(q1);
if (flag1 == 0 || p1 == q1)
{
cout << " Wrong input ";
exit(1);
}
cout << " Enter the message that you want to encrypt ";
fflush(stdin);
cin >> msg1;
for (i = 0; msg1[i] != ''; i++)
m1[i] = msg1[i];
n1 = p1 * q1; //here is the logic of RSA algorithm
t1= (p1 - 1) * (q1 - 1);
cei();
cout << " Possible values of e1 and d1 are ";
for (i = 0; i < j - 1; i++)
cout << e1[i] << " " << d1[i] << " ";
encrypting();
decrypting();
return 0;
}
void cei()
{
int k1;
k1= 0;
for (i = 2; i < t1; i++)
{
if (t1 % i == 0)
continue;
flag1 = primechk(i);
if (flag1 == 1 && i != p1 && i != q1)
{
e1[k1] = i;
flag1 = cd1(e1[k1]);
if (flag1 > 0)
{
d1[k1] = flag1;
k1++;
}
if (k1 == 99)
break;
}
}
}
long int cd1(long int x)
{
long int k1 = 1;
while (1)
{
k1 = k1 + t1;
if (k1 % x == 0)
return (k1 / x);
}
}
void encrypting()
{ char ch;
long int pt1, ct1, key = e1[0], k1, len1;
i = 0;
len1 = strlen(msg1);
while (i != len1)
{
pt1 = m1[i];
pt1 = pt1 - 96;
k1 = 1;
for (j = 0; j < key; j++)
{
k1 = k1* pt1;
k1 = k1 % n1;
}
temp1[i] = k1;
ct1 = k1 + 96;
en1[i] = ct1;
i++;
}
en1[i] = -1;
cout << " The new encrpted massage is ";
for (i = 0; en1[i] != -1; i++){
ch=en1[i];
cout<<ch;
}
}
void decrypting()
{ char ch;
long int pt1, ct1, key = d1[0], k1;
i = 0;
while (en1[i] != -1)
{
ct1 = temp1[i];
k1 = 1;
for (j = 0; j < key; j++)
{
k1 = k1* ct1;
k1 = k1 % n1;
}
pt1 = k1 + 96;
m1[i] = pt1;
i++;
}
m1[i] = -1;
cout << " THE DECRYPTED MESSAGE IS ";
for (i = 0; m1[i] != -1; i++){
ch=m1[i];
cout<<ch;
}
}
input:
5
7
jhonjack
output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.