问题描述
我发誓,每次我查看MSDN文档时,这种情况似乎都在改变.当我对可执行文件进行编码时,Microsoft建议使用 RtlGenRandom API可以生成具有加密强度的随机数.
I swear, this seems to be changing every time I check the MSDN documentation. When I coded my executable Microsoft was suggesting to use RtlGenRandom API to generate cryptographically strong random numbers.
现在,当我检查RtlGenRandom的文档时,那里的注释建议使用 CryptGenRandom 代替.但是,随后针对CryptGenRandom的另一个注释指出了这一点:
Now when I'm checking documentation for RtlGenRandom, the note there suggests using CryptGenRandom instead. But then another note for CryptGenRandom states this:
那么有人可以在C中显示一个示例,说明如何使用那些"下一代密码学"来生成Microsoft现在推荐的字节数组.
So can someone show an example in C of how to use those "Cryptography Next Generation APIs" to generate a byte array of random numbers that Microsoft recommends now?
推荐答案
这并不重要,在Windows XP和更高版本上,默认提供程序最终会调用相同的函数.2000和未修补XP上的RNG主要在内部使用SHA1 + RC4,它具有一些安全问题.
It does not really matter, on Windows XP and later the default providers end up calling the same function. The RNG on 2000 and unpatched XP mainly use SHA1+RC4 internally and it has some security issues.
我刚刚在Windows 8上做了一些实验,这就是我发现的:
I just did some experiments on Windows 8 and this is what I found:
-
RtlGenRandom
(又名advapi32!SystemFunction036
)调用CRYPTBASE!SystemFunction036
>>>bcryptPrimitives!ProcessPrng
>>>bcryptPrimitives!AesRNG *
. -
CryptGenRandom
调用CRYPTSP!CryptGenRandom
>>>%provider%!CPGenRandom
>>>CRYPTBASE!SystemFunction036
.在我的测试中,%provider%
是rsaenh或dssenh,但如果您专门要求第三方提供商,则可能是不同的实现方式. -
BCryptGenRandom
调用bcryptPrimitives!MSCryptGenRandom
>>>bcryptPrimitives!GenRandomAes
>>>bcryptPrimitives!AesRNG *
BCRYPT_RNG_ALGORITHM
CNG算法标识符(BCRYPT_RNG_DUAL_EC_ALGORITHM
最终以bcryptPrimitives!GenRandomDualEcc
结尾).
RtlGenRandom
(AKAadvapi32!SystemFunction036
) callsCRYPTBASE!SystemFunction036
>>>bcryptPrimitives!ProcessPrng
>>>bcryptPrimitives!AesRNG*
.CryptGenRandom
callsCRYPTSP!CryptGenRandom
>>>%provider%!CPGenRandom
>>>CRYPTBASE!SystemFunction036
.%provider%
was rsaenh or dssenh in my tests but could possibly be a different implementation if you specifically ask for a 3rd-party provider.BCryptGenRandom
callsbcryptPrimitives!MSCryptGenRandom
>>>bcryptPrimitives!GenRandomAes
>>>bcryptPrimitives!AesRNG*
with theBCRYPT_RNG_ALGORITHM
CNG Algorithm Identifier (BCRYPT_RNG_DUAL_EC_ALGORITHM
ends up inbcryptPrimitives!GenRandomDualEcc
instead).
这当然是未记录的实现细节,可以更改,但是我真的不认为您需要担心选择哪个功能.如果您的目标是Vista +,则可以使用BCrypt. CryptGenRandom
永远不会被删除,它会破坏太多的应用程序,如果支持<Vista.
This is of course undocumented implementation details that could change but I don't really think you need to worry about which function you pick. If your target is Vista+ you can use BCrypt. CryptGenRandom
will never be removed, it would break too many applications and you should pick it if you support < Vista.
这篇关于RtlGenRandom/CryptGenRandom或其他WinAPI生成加密安全的随机数(2018年第一季度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!