Hello, I'm really close to having all the pieces I need for my program. The last piece that is causing me issues is RSA Encrypt/Decrypt.
This is what I have so far (I put the class info this time):
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
|
std::string PGDCrypto::doRSADecryption(std::string message) {
std::string out = "";
std::string correctedMessage = pgd.hexToStr(message);
//int length = correctedMessage.length();
unsigned char * decrypted = (unsigned char *)malloc(1024);
unsigned char * in = (unsigned char *)(correctedMessage.c_str());
pgd.clientKey = pgd.StoreDataStruct();
RSA_private_decrypt(RSA_size(pgd.clientKey), in, decrypted, pgd.clientKey, RSA_PKCS1_OAEP_PADDING);
//
ofstream outstrm("RSATest.txt", ios::app);
outstrm << "cnv: " << in << "\n";
outstrm << "dec: " << decrypted << "\n\n\n";
outstrm.close();
//
out.append(string((const char *)decrypted));
free(in);
free(decrypted);
return out;
}
std::string PGDCrypto::doRSAEncryption(std::string message) {
std::string out = "";
int length = message.length();
unsigned char *encrypted = (unsigned char*)malloc(1024);
unsigned char * in = (unsigned char *)(message.c_str());
pgd.clientKey = pgd.StoreDataStruct();
RSA_public_encrypt(length, in, encrypted, pgd.clientKey, RSA_PKCS1_OAEP_PADDING);
//
ofstream outstrm("RSATest.txt", ios::app);
outstrm << "org: " << encrypted << "\n";
outstrm.close();
//
out.append(pgd.strToHex(string((const char *)encrypted)));
//
free(in);
free(encrypted);
//
return out;
}
std::string PGDCrypto::hexToStr(std::string &input)
{
std::ostringstream output;
int len = input.length();
char tmp[5], *ptr;
tmp[0] = '0';
tmp[1] = 'x';
tmp[4] = '\0';
for (int i = 0; i < len; i += 2)
{
if (i + 1 >= len)
return output.str();
tmp[2] = input[i];
tmp[3] = input[i+1];
//std::cout << tmp << "\n";
output << (char)strtol(tmp, &ptr, 16);
}
return output.str();
}
std::string PGDCrypto::strToHex(std::string &input)
{
std::ostringstream output;
int len = input.length();
int val;
//std::ofstream outfile("tohex.txt", ios::out);
for (int i = 0; i < len; i++)
{
val = (int)input[i];
//std::cout << val << "\n" << input[i] << "\n\n";
if (val < 0)
val = 256 + val;
if (val < 16)
output << "0"; // >_>
output << std::hex << val;
}
return output.str();
}
|
I have already checked the clientKey variable, everything is in it's proper place there (from the StoreDataStruct function).
The function that I'm generating the data to encrypt/decrypt is as follows:
1 2 3 4 5 6 7 8 9 10 11 12
|
std::string PGDCrypto::generateChallenge(int length) {
std::string challenge = "";
std::string hexclg = "0123456789abcdef";
std::string storage = "";
for(int i = 0; i < length; i++) {
int randNum = (int)((16 * rand()) / RAND_MAX);
//cout << "RND: " << randNum << endl;
storage = hexclg.substr(randNum, 1);
challenge.append(storage);
}
return challenge;
}
|
The main issues with this currently is with the decoded data, it decodes a portion of it, but the rest remains encrypted (ASCII), like so: (test dump):
dec: b8960b19d7b76c85fa252bf9050a2dd9bd02b93129555acc89ce2a642d169483а8i#µºÉÉ9p¢//z¾©ý]ÏÿåÄ8—†
D<,‹6`λÒåTÿx–bµ”û[cíêd6/Åï¥|m]hzÿ]hzÿ]hzÿ>EQÿ
The other thing is with the function that calls it, after I call the function about 3 - 4 times, the output is all ASCII:
dec: ÿÿíæ²4zÜ/cW;E],JF`ÌÿÌ\B^„]«Qäð",½PóźÝÃA+RX"ÈM&.áÇt[
~¾³à”±-z“6w´§ÁÈ+?¼³0¡:sŒ¼äP‹³9˜ß¿wO:Ö¼dùj¡-
How can I go about getting this fixed up?