Self taught, wondering why this isn't working

In school, we were given an assignment to write an essay in an interesting way, so I decided to program one in c++. To make it impossible to read the essay without compiling the code, I took the essay and converted it from ASCII to decimal and encoded it with some RSA just for good measure. I want the program to be able to decode the RSA and then convert the decimal back to ASCII. The essay array would include a separate string of numbers for each line (I only included one line for simplicity), while the pvalue, qvalue, and dvalue arrays would hold the RSA values needed to decrypt that line. I thought that this program would work, but when I run it (in console), it doesn't return anything.

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
#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;

//var declarations
string essay [1] = {"92138400690223138600690250690091000250418400507607501468001684841300682250628483003490002368006902840090848476231386003490006923778400687623555523138600500750141100763434752313860050690050"};
int pvalue [1] = {5};
int qvalue [1] = {19};
int dvalue [1] = {29};
int c = 0;
size_t n = 0;
string scontainer;
int icontainer;
string RSAresult = "";

string RSAdecoder(string l, int p, int q, int d);
int decconverter(string l);

//main function: handles output
int main()
{
	while (c < 1){
		scontainer = RSAdecoder (essay[c], pvalue[c], qvalue[c], dvalue[c]);
		decconverter (scontainer);
		c++;
	}
	system("PAUSE");
}

//RSAdecoder: decodes individual essay lines
string RSAdecoder(string l, int p, int q, int d)
{
	n = 0;
	scontainer = "";
	while (n <= l.length()){
		scontainer = l.substr(n, 2);
		icontainer = atoi(scontainer.c_str());
		icontainer = (icontainer ^ d) % (p * q);
		RSAresult = RSAresult + to_string(icontainer);
		n = n + 2;
	}
	return RSAresult;
}

//decconverter: converts decoded lines to ASCII
int decconverter(string l)
{
	while (n < l.length()){
		scontainer = l.substr(n, 2);
		icontainer = atoi(scontainer.c_str());
		cout << char(icontainer + 32);
		n = n + 2;
	}
	cout << "" << endl;
	return 0;
}


Just in case any of the variables didn't make sense immediately from the code, I'll explain what each one does quickly:

essay: array that stores the different lines of the essay
pvalue: stores the value for p for RSA decryption
qvalue: stores the value for q for RSA decryption
dvalue: stores the value for d for RSA decryption
c: defines which line is being worked on (<1 in this case because the first array value is 0 and there's only one value)
n: defines a specific spot in a substring if one is being worked with
scontainer: contains miscellaneous strings used during the program
icontainer: contains miscellaneous ints used during the program
RSAresult: stores the completely decoded RSA result for that line

Thanks for the help!
Last edited on
The problem is probably line 40:

icontainer = (icontainer ^ d) % (p * q);

^ is the bitwise XOR operator. If you want powers you need the power function (pow).

However, I can't be sure. What do you mean by the "program is not returning"?

Also, a style thing. Try not to use so many global variables like you have; you should try to limit the scope of variables to the smallest possible.
You build up some encoding in icontainer, then append it to RSAresult. However, the encoding is a variable length string and you append them in RSAresult without a delimiter. So you have no idea how to decode this string, because you don't know where each icontainer entry starts/stops.

Your decode assumes they're 2 bytes, but they may not be.

Also, although RSA uses two prime numbers (p1, p2), the calculations use on p1-1, p2-1 and p1*p2.
http://en.wikipedia.org/wiki/RSA_%28cryptosystem%29#Key_generation
I'm running the program from Visual Basic, and the console output only shows several blank lines and the standard "Press any key to continue...". That's what I mean by not getting an output.

How can I avoid using less global variables? Do I define them inside of the specific function in which they're used?

How can I make sure that every encoding would be exactly two bytes then? Would I do something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//RSAdecoder: decodes individual essay lines
string RSAdecoder(string l, int p, int q, int d)
{
	n = 0;
	scontainer = "";
	while (n <= l.length()){
	        scontainer = l.substr(n, 2);
		icontainer = atoi(scontainer.c_str());
		icontainer = pow(icontainer, d) % (p * q)
		scontainer = to_string(icontainer);
                if (scontainer.length() = 1){
                        scontainer = "0" + scontainer;
		n = n + 2;
	}
	return RSAresult;
}


And I have a program that can confirm if a (p, q, d, e) pattern works for RSA encryption. It verified that (5, 19, 29, 5) works, and I'll use that program to find other prime pairs that work.
Last edited on
Topic archived. No new replies allowed.