0% found this document useful (0 votes)
33 views4 pages

Iostream Math.h: #Include #Include Using Namespace Int Int Double

This C++ program generates a public/private key pair for RSA encryption. It takes in values p and q, confirms they are prime, calculates n as p * q, calculates the totient and uses it to find the encryption/decryption exponents e and d. It then encrypts a message m by exponentiating it to e modulo n, and decrypts the ciphertext c by exponentiating it to d modulo n, demonstrating the RSA encryption/decryption process works as expected.

Uploaded by

Sarwar Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views4 pages

Iostream Math.h: #Include #Include Using Namespace Int Int Double

This C++ program generates a public/private key pair for RSA encryption. It takes in values p and q, confirms they are prime, calculates n as p * q, calculates the totient and uses it to find the encryption/decryption exponents e and d. It then encrypts a message m by exponentiating it to e modulo n, and decrypts the ciphertext c by exponentiating it to d modulo n, demonstrating the RSA encryption/decryption process works as expected.

Uploaded by

Sarwar Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include<iostream>

#include<math.h>

using namespace std;


int main()
{
int i,p,q,n,e,d,m,x1,x2;
double count=0,countl=0,c,m1;
cout<<"\n\nEnter value of p: \n";
cin>>p;
cout<<"\n\nEnter value of q: \n";
cin>>q;
for(i=2;i<p;i++)
{
if(p%i==0)
{
count++;
break;
}
}
if(count==0)
{
cout<<"\n\np is a prime number\n\n";
}
else
{
cout<<"\n\np is not a prime number\n\n";
}

for(i=2;i<q;i++)
{
if(q%i==0)
{
countl++;
break;
}
}
if(countl==0)
{
cout<<"\n\nq is a prime number\n";
}
else
{
cout<<"\n\nq is not a prime number\n";
}
if((p!=0)&&(q!=0))
n = p *q;
cout<<"\n\nn: "<<n<<"\n";

int t = (p-1) * (q-1);


cout<<"\n\nTotient is: "<<t;

for(int k=1; k<=t; k++)


{
if(t%k!=0)
{
e = k;
break;
}
}
cout<<"\n\ne: "<<e<<"\n";

d=1;
for(int k1=1;k1<t;k1++)
{
if ((k1 * e) % t == 1)
{
d = k1;
break;
}
}
cout << "\n\nd: " << d <<"\n";
cout<<"\n\nd: "<<d<<"\n";

cout<<"\n\nPublic Key: {"<<e<<","<<n<<"}\n";

cout<<"\n\nPrivate Key: {"<<d<<","<<n<<"}\n";

cout<<"\n\nEnter the value of message signal: ";


cin>>m;
x1 = pow(m,e);
c = x1 % n;
cout<<"\n\nEncryption is: "<<c<<"\n";
x2 = pow(c,d);
m1 = x2 % n;
cout<<"\n\nDecryption is: 0"<<m1<<"\n";

You might also like