以下是使用BCryptGenRandom生成随机数的示例代码:

#include <Windows.h>
#include <bcrypt.h>
#include <iostream>

int main()
{
    BCRYPT_ALG_HANDLE hProvider;
    BCryptOpenAlgorithmProvider(&hProvider, BCRYPT_RNG_ALGORITHM, NULL, 0);

    const int BUFFER_SIZE = 16;
    BYTE buffer[BUFFER_SIZE] = { 0 };
    BCryptGenRandom(hProvider, buffer, BUFFER_SIZE, BCRYPT_USE_SYSTEM_PREFERRED_RNG);

    for (int i = 0; i < BUFFER_SIZE; i++)
    {
        std::cout << std::hex << +buffer[i] << " ";
    }

    BCryptCloseAlgorithmProvider(hProvider, 0);

    return 0;
}

在此示例中,打开随机数生成算法提供程序,并使用BCryptGenRandom函数生成长度为16个字节的随机数。最后,关闭算法提供程序。

输出示例:

e7 80 81 32 9c 6a 1d 01 2a 75 a8 09 56 6c 9d 30
``
BCryptGenRandom生成随机数代码示例

原文地址: https://www.cveoy.top/t/topic/clfT 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录