RSA

My practice assignment program should ask the user for the values of p, q, e,and d that are required to encrypt and decrypt a message using RSA encryption. The program can then compute the values of N and φ. Then ask if the user wants to encrypt or decrypt a message. The text to encrypt or decrypt should be provided in a file. Ask the user for the name of the file. The program should write the encrypted or decrypted message to a file named results.txt

However, each time I run it in the testing website our professor assigned us, my code would fail all the testing cases.

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

  //encryption function
int EncVSRSA(int num,int n,int e)
{
   int i, j, val;
   val = 1;
   for(j = 1;j <= e;j++)
   {
       val*= num; // calculating num^e mod n
       val %= n;
       //cout<<val<<endl;
   }
   return(val);

}

//decryption function
int DecVSRSA(int num,int n,int d)
{
   int i,j,val;
   val = 1;
   for(j = 1; j <= d; j++)
   {
       val *= num; // calculating num^e mod n
       val %= n;
       //cout<<val<<endl;
   }
   return(val);
}


//main function
int main()
{
   ifstream ins("results.txt");
   
   int p,q,n,d,e,i,msg;
   char op;
   cout << "Enter value for p and q:";
   cin >> p >> q;
   n = p * q;
   cout << "Enter value for e:";
   cin >> e;
   cout << "Enter value for d:";
   cin >> d;
  
  
   //cout<<"Enter msg:";
   //cin>>msg;
  
   //enter choice
   cout << "Do you want to encrypt(E) or decrypt(D)?";
   cin >> op;
  
   char fname[50];
   cout << "Enter file name:";
   cin >> fname;
  
   //input file name
   ifstream file_in(fname);

   //output file
   ofstream file_out("results.txt");
  
   if(file_in.is_open())
   {
       while(!file_in.eof())
       {
           file_in >> msg;
          
           //Performing encryption
           if(op == 'E')  
           {
               int cyper = EncVSRSA(msg,n,e);
               file_out << cyper <<" ";
           }
          
           //Performing decryption
           else
           {
               int plain = DecVSRSA(msg,n,d);
               file_out << plain << " ";
           }
          
       }
   }
   file_in.close();
   file_out.close();
}
> my code would fail all the testing cases.
So you have no means of testing locally?

> while(!file_in.eof())
This loops one more time than you expect.
Use
while ( file_in >> msg )

> val*= num; // calculating num^e mod n
I'm pretty sure the val %= n; needs to be done only once when the loop is finished.

Also, unless your p,q,e,d are really small, numeric overflow is going to be an issue.
Maybe the problem lies at the format that the file has. I guess that the test files are sampled of consecutive chunks of data. If so, then you need to know how long a chunk of data is.

Here an example of reading a 64-bit chunk:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
std::uint64_t read_chunk( std::istream& is )
{
    std::uint64_t chunk = 0;
    unsigned char byte;

    // reads 8 byte and assembles the integer chunk
    for (int byte_pos = 7; byte_pos >=0; --byte_pos)
    {
        if ( !is.get(byte) ) throw std::exception("Unexpected break off of file stream!");
        chunk += byte;
        if (byte_pos != 0) chunk <<= 8;  // left bit shift by 8 bits
    }
    return chunk;
}
Last edited on
Additional,
> ifstream ins("results.txt");
Opening your output file for input might conflict with your attempt to open it for output.

> However, each time I run it in the testing website our professor assigned us,
You need to be very clear what the automated system does, and expects.
You're basically assuming an interactive user, with all the cout and cin stuff.

But the automated system is going to be something like this, where the I/O is redirected from files, and the results are compared to what is expected. If you're off by a single byte in anything that is checked, you lose.
1
2
3
4
5
6
7
8
9
g++ prog.cpp
if [ $? -eq 0 ]; then
  ./a.out < input.txt > output.txt 2>error.txt
  cmp -s results.txt expected.txt
  if [ $? -ne 0 ]; then
    # test failed
  fi
  # may also check output.txt and error.txt
fi

http://codecheck.it/files/18102820549dtl850v7ecqkvpt78y3bhpd

thats the tester we are using. When I run it on VS it works, but once I run it through this tester it fails.
This makes it somewhat clearer. What values have p, q, e, d, and op (or how will they be passed to the program, and in which order)?
How should the output files be named (or should the output get printed to console)?
Last edited on
Topic archived. No new replies allowed.