BLOG main image
분류 전체보기 (137)
궁시렁 (39)
컴쟁이의 연구실 (65)
리버싱 (17)
배워보자 (3)
좋은 곳 (9)
seyool님의 언팩미#1 들여다보기.
jz in reverse engineering
MS API 드래프트 제 1버전 공개
낚시광준초리의 Blog 세상(IT-Fo..
[Kernel] KeServiceDescriptorTa..
我好电脑,我是一名的程序设计员..
SSDT에 대한 고찰
::거듭나기::
불필요한 관심
^0^range.Net - 신은 불공평하다..
138,103 Visitors up to today!
Today 3 hit, Yesterday 28 hit
daisy rss
^0^range.net
2007/10/29 17:45
CryptGenRandom Function

The CryptGenRandom function fills a buffer with cryptographically random bytes.

BOOL WINAPI CryptGenRandom(
  __in          HCRYPTPROV hProv,
  __in          DWORD dwLen,
  __in_out      BYTE* pbBuffer
);

Parameters

hProv

Handle of a cryptographic service provider (CSP) created by a call to CryptAcquireContext.

dwLen

Number of bytes of random data to be generated.

pbBuffer

Buffer to receive the returned data. This buffer must be at least dwLen bytes in length.

Optionally, the application can fill this buffer with data to use as an auxiliary random seed.

Return Value

If the function succeeds, the return value is nonzero (TRUE).

If the function fails, the return value is zero (FALSE). For extended error information, call GetLastError.

The error codes prefaced by "NTE" are generated by the particular CSP being used. Some possible error codes are listed in the following table.

Return code Description

ERROR_INVALID_HANDLE

One of the parameters specifies a handle that is not valid.

ERROR_INVALID_PARAMETER

One of the parameters contains a value that is not valid. This is most often a pointer that is not valid.

NTE_BAD_UID

The hProv parameter does not contain a valid context handle.

NTE_FAIL

The function failed in some unexpected way.

Remarks

The data produced by this function is cryptographically random. It is far more random than the data generated by the typical random number generator such as the one shipped with your C compiler.

This function is often used to generate random initialization vectors and salt values.

Software random number generators work in fundamentally the same way. They start with a random number, known as the seed, and then use an algorithm to generate a pseudo-random sequence of bits based on it. The most difficult part of this process is to get a seed that is truly random. This is usually based on user input latency, or the jitter from one or more hardware components.

With Microsoft CSPs, CryptGenRandom uses the same random number generator used by other security components. This allows numerous processes to contribute to a system-wide seed. CryptoAPI stores an intermediate random seed with every user. To form the seed for the random number generator, a calling application supplies bits it might have—for instance, mouse or keyboard timing input—that are then added to both the stored seed and various system data and user data such as the process ID and thread ID, the system clock, the system time, the system counter, memory status, free disk clusters, the hashed user environment block. This result is SHA-1 hashed, and the output is used to seed an RC4 stream, which is then used as the random stream and used to update the stored seed. If an application has access to a good random source, it can fill the pbBuffer buffer with some random data before calling CryptGenRandom. The CSP then uses this data to further randomize its internal seed. It is acceptable to omit the step of initializing the pbBuffer buffer before calling CryptGenRandom.

Example Code [C++]

The following example shows the generation of 8 random bytes. These can be used to create cryptographic keys or for any application that uses random numbers. For an example that includes the complete context for this example, see Example C Program: Duplicating a Session Key.

 //--------------------------------------------------------------------
// Declare and initialize variables.

HCRYPTPROV   hCryptProv;
BYTE         pbData[16];

//--------------------------------------------------------------------
//  This code assumes that a cryptographic context has been acquired 
//  For code details, see "Example C Program: Duplicating a Session 
//  Key."

//--------------------------------------------------------------------
// Generate a random initialization vector.

if(CryptGenRandom(
   hCryptProv, 
   8, 
   pbData)) 
{
     printf("Random sequence generated. \n");
}
else
{
     printf("Error during CryptGenRandom.\n");
     exit(1);
}

Requirements

Client

Requires Windows Vista, Windows XP, Windows Me, Windows 2000 Professional, Windows 98, Windows NT Workstation 4.0, or Windows 95 OSR2 and later.

Server

Requires Windows Server 2008, Windows Server 2003, Windows 2000 Server, or Windows NT Server 4.0.

Redistributable

Requires Internet Explorer 3.02 or later on Windows 95.

Header

Declared in Wincrypt.h.

Library

Use Advapi32.lib.

DLL

Requires Advapi32.dll.

See Also

Key Generation and Exchange Functions
CryptAcquireContext
CryptGenKey
CryptSetKeyParam

크리에이티브 커먼즈 라이선스
Creative Commons License
Trackback Address :: http://0range.net/trackback/152 관련글 쓰기
BlogIcon binish | 2009/06/05 13:26 | PERMALINK | EDIT/DEL | REPLY
세율이 블로그에 CryptGenRandom API 내용이 있길래 ^^
저게 이스라엘 대학생이던가?에 의해서 한 번 크랙되었었거든ㅡ
요즘 버전에서는 어떠한가 찾아보던중 방문 =)
누구보다 열심히 하는 세율이 짱 멋지다 ㅎ
BlogIcon seyool | 2009/06/09 22:39 | PERMALINK | EDIT/DEL
좋은 정보 고마워.
아직 난 멀었어.. 더 열심히 해야지 ㅎㅎ
Name
Password
Homepage
Secret