#include<iostream>
#include<conio.h>
#include<math.h>
#include <stdio.h>
usingnamespace std;
int main()
{
int i,j,k,p,q,flag,e,x,d;
// clrscr();
cout<<"\n Enter Value of p :: ";
cin>>p;
cout<<"\n Enter Value of q :: ";
cin>>q;
int n=p*q;
int fn=(p-1)*(q-1);
cout<<"\n Value of (p,q) :: "<<p<<" "<<q;
cout<<"\n Value of n,fn :: "<<n<<" "<<fn;
cout<<"\n\n Select Public Key e (1 < e < "<<fn<<") ::";
cin>>e;
int sk;
for(i=1;i>0;i++)
{
x=i*e;
d=x%fn;
if(d==1)
{
cout<<"\n\n Private Key ::"<<i;
sk=i;
break;
}
}
int pt;
cout<<"\n\n Enter Plain Text ::";
cin>>pt;
int m=1;
for(i=0;i<e;i++)
m=(m*pt)%n;
m=m%n;
cout<<"\n\n Cipher Text ::"<<m;
getch();
k=1;
for(i=0;i<sk;i++)
k=(k*m)%n;
k=k%n;
cout<<"\n\n Decrypted Text ::"<<k;
getch();
return 0;
}
This isn't RSA.
You don't even have to know the algorithm to realize that.
If you're looking for a real implementation, try looking at GPG's source code, or OpenSSL's RSA implementation. You shouldn't expect beginner-friendly or short code, though.
Alternatively, you might try searching for tutorials.
It's part of an RSA implementation, but it has several issues.
1. The key sizes are woefully small. No more than 63 bits on nearly all platforms. At most you can encrypt 8 characters, often less. RSA pretty much requires arbitrary precision arithmetic and large primes.
2. The way you wrote your input statements, you can only work with numeric plaintexts, not text. If you try to enter, say, "hello", the plaintext variable will stay uninitialized.
3. The primality of p and q is not verified. Nothing works without this property.
4. The private key is only ever 1. The private key is derived by first picking e, which should be a random number between 2 and the totient - 1 (the totient in your code is called 'fn') such that it is coprime with the totient. The private key is then the multiplicative inverse of e modulo totient - 1. The multiplicative inverse is computed with the extended Euclidean algorithm.
5. Prior to encrypting, the plain text should be checked to ensure it is no coprime with p*q. If it's coprime, it needs to be modified.