RSA encryption program help

I'm working on a project for one of my courses and I'm stuck. We're suppose to develop a program that can generate an RSA key pair, encrypt and decrypt files. I'm pretty new to c++ and I believe what I have so far is right. I believe all I have left to do is do the encryption but I'm not sure how to do that. Any help would be great


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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

typedef unsigned int s64;
typedef unsigned long long int u64;
typedef long long int s64;

// insert functions like isPrime, extEuclid, etc. here

bool isPrime(s64 n) {

  if (n == 2)
    return true;
  if (n % 2 == 0)
    return false;
  for (s64 i=3;i*i<=n;i+=2)
    if (n % i == 0)
      return false;
  return true;
}

s64 extEuclid(s64 a,s64 b,s64 &lastX,s64 &lastY) {
  s64 x,y,q,tmp;

  x = lastY = 0;
  y = lastX = 1;

  while (b != 0) {
    q = a / b;

    tmp = a % b;
    a = b;
    b = tmp;

    tmp = lastX - q * x;
    lastX = x;
    x = tmp;

    tmp = lastY - q * y;
    lastY = y;
    y = tmp;
  }

  return a;
}

// functions that aren't completed yet

void doEncrypt(s64 n,s64 e,char *inFileName,char *outFileName) {

  cout << "In doEncrypt(" << n << ',' << e << ','        // ./project3 -e n... e... junkfile junk.enc
       << inFileName << ',' << outFileName << ")\n";     // ./project3 -d n... d... junk.enc test   if(!inFile)

  // open files and make sure that succeeds

  // loop forever

    // read three characters. if first read fails, stop loop
    // if second and/or third fail, use 0 for them
    // use the .get() method

    // convert to number (m), see problem spex

    // compute M = (m^e) % n. use modExp(m,e,n)

    // output M with a space or \n after

  // end of loop

  // close the files
}

void doDecrypt(s64 n,s64 d,char *inFileName,char *outFileName) {

  cout << "In doDecrypt(" << n << ',' << d << ','
       << inFileName << ',' << outFileName << ")\n";

  // open files and make sure that succeeds

  // loop forever

    // read M from file

    // compute m = (M^d) % n. use modExp(M,d,n)

    // extract three characters from m.
    // output any of them that are not 0

  // end of loop

  // close the files
}

void keyGen(s64 &n,s64 &e,s64 &d) {
  s64 p,q,f,x,y;

  // randomly choose p

  do {
    p = rand() % 61423 + 4099;
  } while (!isPrime(p));

  // randomly choose q

  do {
    q = rand() % 61423 + 4099;
  } while (!isPrime(q));

  // compute n

  n = p * q;

  // compute f

  f = (p - 1) * (q - 1);

  // randomly choose e

  do {
    e = rand() % 61423 + 4099;
  } while (extEuclid(e,f,x,y) != 1);

  // compute d

  extEuclid(e,f,d,y);

  cout << "In keyGen()\n";

  // to test this...

  // output p,q,n,e,f,d

  cout << "Testing...\np = " << p << "\nq = " << q
       << "\nn = " << n << "\ne = " << e << "\nf = "
       << f << "\nd = " << d << endl;
  // p and q should be random, prime and between 4099 and 65521
  // n should be p * q
  // f should be (p-1)*(q-1)
  // e should be random, between 4096 and 65535 and gcd(e,f) == 1
  // (d*e)%f should be 1
}

// functions that are done

bool validateInt(char *str,s64 &n) {
  int i;

  for (i=0;str[i]!=0;i++)	// look to make sure all are digits
    if (!isdigit(str[i]))	// no digit? Stop now.
      break;

  if (str[i] == 0) {		// if we make it to the end, good
    n = strtoul(str,NULL,10);
    return true;
  } else			// otherwise, not valid
    return false;
}

void doKeyGen(void) {
  s64 n,e,d;

  keyGen(n,e,d);

  cout << " Public key: " << n << "   " << e << endl;
  cout << "Private key: " << n << "   " << d << endl; 
}

int main(int argc,char *argv[]) {
  bool optionsOk = false;
  s64 n,e,d;

  srand(time(NULL));

  if (argc == 2) {	// looking for -k
    if (argv[1][0] == '-' && argv[1][1] == 'k') {
      doKeyGen();
      optionsOk = true;
    }
  }
  if (argc == 6) {	// looking for -d or -e
    if (argv[1][0] == '-') {
      if (argv[1][1] == 'e') {
        if (validateInt(argv[2],n) && validateInt(argv[3],e)) {
          doEncrypt(n,e,argv[4],argv[5]);
          optionsOk = true;
        }
      }
      if (argv[1][1] == 'd') {
        if (validateInt(argv[2],n) && validateInt(argv[3],d)) {
          doDecrypt(n,d,argv[4],argv[5]);
          optionsOk = true;
        }
      }
    }
  }

  if (!optionsOk) {
    cout << "Usage: project3 -k\n"
            "       project3 -e public-n public-e input-file output-file\n"
            "       project3 -d public-n private-d input-file output-file\n";
  }

  return 0;
}
Topic archived. No new replies allowed.