A problem in RSA code

Hi.
I searched on Internet to find RSA implementation, finally I found that code, but the problem is ,it doesn't give cipher text.
Can someone help?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<iostream>
#include<conio.h>
#include<math.h>
#include <stdio.h>

using namespace 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.

A quick search found this article, which seems promising, although it doesn't use C++.
http://sahandsaba.com/cryptography-rsa-part-1.html
I wrote a naive RSA implementation a few months ago: https://gist.github.com/Helios-vmg/50d2c062333bdf810ffbbdcc46157cb3
It needs my bignum library: https://github.com/Helios-vmg/EasyBigNum/
Note: My prime generator is as basic as it gets. It doesn't hold a candle to what you can find in proper cryptographic libraries, in terms of speed. You'll want to run it in a powerful computer if you ever want to see it finish.

The algorithm itself is relatively simple, if you're familiar with number theory.
Thanks both for your answers, but what is the problem with the code?
If it's not RSA, so what is it?
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.
Ok, Helios. Thanks a lot.
Topic archived. No new replies allowed.