CryptGenRandom windows下生成随机数的函数使用
2014-10-02 11:36
573 查看
// test_CryptGenRandom.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "windows.h"
#include "wincrypt.h"
#include <iostream>
using namespace std;
class CCryptRandom
{
public:
BOOL Random(void *lpBuffer,DWORD dwLen);
CCryptRandom();
virtual~CCryptRandom();
private:
HCRYPTPROV hCryptProv;
};
CCryptRandom::CCryptRandom()
{
hCryptProv=NULL;
CryptAcquireContext((HCRYPTPROV*)&hCryptProv,NULL,NULL,PROV_RSA_FULL,0);
}
CCryptRandom::~CCryptRandom()
{if(hCryptProv!=NULL)CryptReleaseContext(hCryptProv,0);
}
BOOL CCryptRandom::Random(void *lpBuffer, DWORD dwLen)
{
if(hCryptProv==NULL)return FALSE;
BOOL bRet=CryptGenRandom(hCryptProv,dwLen,(BYTE*)lpBuffer);///reinterpret_cast<LPBYTE>lpBuffer);
return bRet;
}
int _tmain(int argc, _TCHAR* argv[])
{
CCryptRandom crypt;
BYTE byte[MAX_PATH];
BOOL bReturnValue = crypt.Random(&byte, MAX_PATH);
for (DWORD i = 0; i < 8; ++i)
std::cout << std::dec << static_cast<unsigned int>(byte[i]) << std::endl;
return 0;
}
//
#include "stdafx.h"
#include "windows.h"
#include "wincrypt.h"
#include <iostream>
using namespace std;
class CCryptRandom
{
public:
BOOL Random(void *lpBuffer,DWORD dwLen);
CCryptRandom();
virtual~CCryptRandom();
private:
HCRYPTPROV hCryptProv;
};
CCryptRandom::CCryptRandom()
{
hCryptProv=NULL;
CryptAcquireContext((HCRYPTPROV*)&hCryptProv,NULL,NULL,PROV_RSA_FULL,0);
}
CCryptRandom::~CCryptRandom()
{if(hCryptProv!=NULL)CryptReleaseContext(hCryptProv,0);
}
BOOL CCryptRandom::Random(void *lpBuffer, DWORD dwLen)
{
if(hCryptProv==NULL)return FALSE;
BOOL bRet=CryptGenRandom(hCryptProv,dwLen,(BYTE*)lpBuffer);///reinterpret_cast<LPBYTE>lpBuffer);
return bRet;
}
int _tmain(int argc, _TCHAR* argv[])
{
CCryptRandom crypt;
BYTE byte[MAX_PATH];
BOOL bReturnValue = crypt.Random(&byte, MAX_PATH);
for (DWORD i = 0; i < 8; ++i)
std::cout << std::dec << static_cast<unsigned int>(byte[i]) << std::endl;
return 0;
}
相关文章推荐
- 给你一个能生成1到5随机数的函数,用它写一个函数生成1到7的随机数。 (即,使用函数rand5()来实现函数rand7())
- C语言每日一函数之2:生成随机数(含STM32/STM8使用AD获取随机数)
- c++ 生成随机数rand()函数的使用:设定范围
- Oracle中生成随机数的函数
- windows.h下几个函数的使用
- 使用Random函数产生随机数
- [NET]Net中的反射使用入门(根据类名和函数名,生成和调用对象的成员函数)
- windows.moveBy()函数使用方法示例
- 同步机制及windows同步函数的使用
- 在Windows下使用VC++6.0编译器生成Qt应用程序
- 使用c#生成不重复的随机数
- 在Windows下编译sqlite3生成动态链接库并使用之
- 标准库rand()函数的缺陷以及Blitz++随机数生成的简介
- Windows程序调试系列: 使用VC++生成调试信息
- Windows程序调试系列: 使用VC++生成调试信息
- ASP生成大小写字母与数字混合随机数的函数
- 同步机制及windows同步函数的使用
- windows.moveBy()函数使用方法示例
- 生成任意位随机数的函数
- 使用VB生成没有限制的函数DLL zz