RtlGenRandom/CryptGenRandom或其他WinAPI生成加密安全的随机数(2018年第一季度) [英] RtlGenRandom/CryptGenRandom or other WinAPI to generate cryptographically secure random numbers (first quarter of 2018)

查看:80
本文介绍了RtlGenRandom/CryptGenRandom或其他WinAPI生成加密安全的随机数(2018年第一季度)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发誓,每次我查看MSDN文档时,这种情况似乎都在改变.当我对可执行文件进行编码时,Microsoft建议使用

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的文档时,那里的注释建议使用

Now when I'm checking documentation for RtlGenRandom, the note there suggests using CryptGenRandom instead. But then another note for CryptGenRandom states this:

重要提示:不推荐使用此API.新的和现有的软件应开始使用加密下一代API .微软可能会删除此API在将来的版本中.

Important: This API is deprecated. New and existing software should start using Cryptography Next Generation APIs. Microsoft may remove this API in future releases.

那么有人可以在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 (AKA advapi32!SystemFunction036) calls CRYPTBASE!SystemFunction036 >>> bcryptPrimitives!ProcessPrng >>> bcryptPrimitives!AesRNG*.
  • CryptGenRandom calls CRYPTSP!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 calls bcryptPrimitives!MSCryptGenRandom >>> bcryptPrimitives!GenRandomAes >>> bcryptPrimitives!AesRNG* with the BCRYPT_RNG_ALGORITHM CNG Algorithm Identifier (BCRYPT_RNG_DUAL_EC_ALGORITHM ends up in bcryptPrimitives!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年第一季度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆