What's the best library to generate random numbers for cryptographic keys in .NET? [closed]
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 months ago.
Improve this questionIn .NET, you can generate RSA key pairs using RSACryptoServiceProvide. But is that the best option in .NET to generate truly rand开发者_高级运维om numbers? There are tons of other open source libraries, such as CryptoPlusPlus, that can be used to generate random number. I would like to hear from other experts' opinions. Thanks in advance for your two cents.
I wouldn't personally go anywhere other than RNGCryptoServiceProvider unless somebody provides a proof that it sucks.
The actual implementation used by this class is not obtainable because it's implemented as part of the runtime (System.Security.Cryptography.Utils._GetBytes
or System.Security.Cryptography.Utils._GetNonZeroBytes
) which, for me, is heartening since I would hope that the implementation would bring at least ambient information in from the host to generate the data.
I do believe these are routed through to lower level win32 apis which, of course, would be patched if a security hole is found in them (well... hopefully!)
How it's done in Mono, however, I wouldn't know.
For the extremely paranoid
The real problem with any of this is that your randomness in .Net can be broken by a malicious hacker by changing the host behaviour (that is - by writing a modified runtime). This is much easier in a VM-style platform like .Net or Java than it is in native code (which would require hacking the lower-level platform or modifying the hardware).
Curiously, using RNGCryptoServiceProvider makes this a lot easier since these aforementioned internal calls are implicitly trusted by the managed part of the .Net framework.
However - in reality - your code is going to have to be something really special for someone to want to go to all that effort. So personally I'd still use RNGCryptoServiceProvider.
But it's why I have a real problem with attempts a crypto libraries and functions written in javascript - a quick browser hack can screw it all.
Classes derived from RandomNumberGenerator
generate truly random numbers.
You could get them from random.org if your project allows that. That should be random enough.
精彩评论