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!