首页 > 其他分享 >使用 CNG API 生成伪随机数

使用 CNG API 生成伪随机数

时间:2022-12-13 16:02:35浏览次数:46  
标签:Status BufferSize Buffer random CNG API 随机数 DWORD include


//
//
// File: RandomNumGeneration.cpp
//
// Contents: This sample shows random number generation in CNG.
//
//

#define WIN32_NO_STATUS
#include <windows.h>
#undef WIN32_NO_STATUS

#include <winternl.h>
#include <ntstatus.h>
#include <winerror.h>
#include <stdio.h>
#include <bcrypt.h>
#include <sal.h>

//
// Utilities and helper functions
//

//----------------------------------------------------------------------------
//
// ReportError
// Prints error information to the console
//
//----------------------------------------------------------------------------
void
ReportError(
_In_ DWORD dwErrCode
)
{
wprintf( L"Error: 0x%08x (%d)\n", dwErrCode, dwErrCode );
}

//-----------------------------------------------------------------------------
//
// wmain
//
//-----------------------------------------------------------------------------
DWORD
__cdecl
wmain(
_In_ int argc,
_In_reads_(argc) LPWSTR argv[]
)
{
NTSTATUS Status;

BYTE Buffer[128];
DWORD BufferSize;

BufferSize = sizeof (Buffer);
memset (Buffer, 0, BufferSize);

//
// Fill the buffer with random bytes
//

Status = BCryptGenRandom (
NULL, // Alg Handle pointer; NUll is passed as BCRYPT_USE_SYSTEM_PREFERRED_RNG flag is used
Buffer, // Address of the buffer that recieves the random number(s)
BufferSize, // Size of the buffer in bytes
BCRYPT_USE_SYSTEM_PREFERRED_RNG); // Flags

if( !NT_SUCCESS(Status) )
{
ReportError(Status);
goto cleanup;
}

Status = STATUS_SUCCESS;

cleanup:

return (DWORD)Status;

UNREFERENCED_PARAMETER( argc );
UNREFERENCED_PARAMETER( argv );
}


标签:Status,BufferSize,Buffer,random,CNG,API,随机数,DWORD,include
From: https://blog.51cto.com/u_15911341/5934383

相关文章

  • API Hooking revealed part 3 and 4 - Thread Deadlock Detector
    APIHookingrevealedpart3and4-ThreadDeadlockDetector Downloadsourcefiles-124KbDownloaddemoproject-225KbIntroductionThisi......
  • 通过API操作阿里云ECS(开关机)
    场景:定时开关机ECS,节省模式关机完整代码示例官方链接:https://next.api.aliyun.com/api-tools/sdk/Ecs?version=2014-05-26&language=go-tea关机:https://next.api.aliyun......
  • 非maven项目使用阿里云短信服务API
    不说废话,先上代码单发/***单发短信*@paramphone手机号*@paramcode模板code*@return*@throwsClientException*/......
  • vue全局API
    ​​Vue.extend(options)​​参数:​​{Object}options​​用法:使用基础Vue构造器,创建一个“子类”。参数是一个包含组件选项的对象。​​data​​​ 选项是特例,需要注......
  • 对vue的api的研究
    ​​Vue.config​​ 是一个对象,包含Vue的全局配置。可以在启动应用之前修改下列属性:​​silent​​类型:​​boolean​​默认值:​​false​​用法:Vue.config.silent=tru......
  • 给ASP.NET Core WebAPI添加Swagger支持
    ASP.NETCoreWebAPI是开发WebAPI接口的有利武器,且由于拥有.NETCore的基因支持跨平台,是当前.NET中开发接口的有利武器。但一般来说WebAPI接口开发完毕后,在发布前还需要测......
  • 优雅的API接口设计
    前言在实际工作中,我们需要经常跟第三方平台打交道,可能会对接第三方平台API接口,或者提供API接口给第三方平台调用。那么问题来了,如果设计一个优雅的API接口,能够满足:安全......
  • HarmonyOS实现登录页面(三)相关api的实现User类和ApiController类(IDEA)
    User类@Data使用@Data注解的主要作用是提高代码的简洁,使用这个注解可以省去代码中大量的get()、set()、toString()等方法;@AllArgsConstructor使用用这个注解修......
  • 【JAVA笔记】Java中的常用工具API简介、Object类的特点、JavaBean类重写Object类中的
    一、Java常用工具API简介   根据步骤查找API文档使用对应功能API网址:https://www.apiref.com/java11-zh/index.html什么是API?二、Object类的特点1.java.lan......
  • nginx https作网关配置webapi路由规则
    为何有这篇文章?因为我有多个小程序,分别调用不同的api站点,服务器只能安装一个https单域名证书。 1、原webapi接口部署完毕,接口地址比如​​http://www.zyiz.net/api/getarti......