Heap Error in Random String Generation

It seems like I always run into something else with my system when I fix something, lol. But looking at what needs to be fixed, there is very few left to do.

My current issue lies with a random string generator, I'm basically generating a random 64 bit length hex string, and testing it's encode/decode with RSA until it comes out correctly (nobody answered my RSA enc/dec thread, so I improvised with this method)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool PGDCrypto::isBadCrypt(std::string result) {
   bool bad = false;
   if(result.compare("ERR_LENGTH_SIZE_TOO_LONG") == 0) {
      bad = true;
   }
   return bad;
}

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;
}


here is a small piece of the function where this is used.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
   std::string challenge = pgd.generateChallenge(64);
   char * input = (char *)malloc(64);
   strcpy(input, (char *)challenge.c_str());
   printf("Initial: %s", input);
   std::string encoded = pgd.doRSAEncrpytion(input, false); 
   std::string decoded = pgd.doRSADecryption((char *)encoded.c_str(), 64, false);
   printf("decode: %s", decoded);
   //
   while(pgd.isBadCrypt(decoded) == true) {
	  printf("Bad Crypt for %s", decoded);
      input = (char *)(pgd.generateChallenge(64).c_str());
      encoded = pgd.doRSAEncrpytion(input, false); 
	  decoded = pgd.doRSADecryption((char *)encoded.c_str(), 64, false);
   }


About the only changes to my RSA functions is that it returns "ERR_LENGTH_SIZE_TOO_LONG" if the decoded length does not equal the second argument, which is assumed to be known.
I'm wondering if it would work for me to re-write my generate function to generate a random 64 bit integer and then using itoa to convert it to hex?

Input, please?

EDIT:

I also forgot to mention this. I did some research and found that the .NET framework contains some RSA functions as well, would that benefit me to use the .NET framework RSA methods, instead of openSSL? if so, is there some good key-generation examples out there?
Last edited on
Topic archived. No new replies allowed.