I have a problem regarding a random number function under Windows. I try to port the following code from linux to windows:
working under linux:
int len = 32;
unsigned char * key = (unsigned char *) malloc(len);
int randomData = open("/dev/random", O_RDONLY);
size_t randomDataLen = 0;
while (randomDataLen < len) {
ssize_t res = read(randomData, key + randomDataLen, len - randomDataLen);
if (res < 0) {
strcpy(result[0],"Cannot read /dev/random\n");
return(1);
}
randomDataLen += res;
}
close(randomData);
Now my windows version of the above code:
HCRYPTPROV p23;
unsigned char * key = (unsigned char *) malloc(len);
size_t randomDataLen = 0;
while (randomDataLen < len) {
int len = 32;
if (CryptAcquireContext(&p23, NULL, NULL,
PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) == FALSE) {
qDebug("CryptAcquireContext failed");
return 1;
}
if (CryptGenRandom(p23, sizeof(unsigned char), (BYTE *)key+randomDataLen) ==FALSE) {
qDebug() << "Randomgenerator failed";
return 1;
}
randomDataLen++;
}
So when I insert it into the rest of the programm it crashes due to a "Invalid address specified to RtlFreeHeap( 1B660000, 2B3E0DF8 )" problem. I have found out through debuging the code, that the root cause is my wrong random number part of the code. Can you please help me to find the problem in my windows code? Or is there somebody who knows a better way to port the linux code mentioned above? Lots of thanks in advance. Andi
Aucun commentaire:
Enregistrer un commentaire