简体   繁体   English

Windows下用于密钥的Randomnumbergenerator-从Linux移植

[英]Randomnumbergenerator for Keys under Windows - ported from Linux

I have a problem regarding a random number function under Windows. 关于Windows下的随机数功能,我遇到了问题。 I try to port the following code from linux to windows: 我尝试将以下代码从linux移植到Windows:

working under linux: 在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: 现在我的Windows版本上面的代码:

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. 因此,当我将其插入到程序的其余部分时,由于“为RtlFreeHeap(1B660000,2B3E0DF8指定的地址无效)”而导致崩溃。 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? 您能帮我在Windows代码中找到问题吗? Or is there somebody who knows a better way to port the linux code mentioned above? 还是有人知道移植上述Linux代码的更好方法? Before someone asks later in code is a free() function. 在有人稍后询问代码之前,是一个free()函数。 Just in case ;) Lots of thanks in advance. 以防万一;)提前表示感谢。 Andi 和我

在Windows版本的代码中, lenmalloc(len)之前未初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM