I cannot find the System.Security.Cryptography.RNGCryptoServiceProvider class in .NetCore.
It is essential to the application I am trying to port from .Net Framework, as it is being used to generate an initialisation vector for encryption.
Does it exist under a different name, or is there another way of achieving this functionality?
The RNGCryptoServiceProvider returns random numbers in the form of bytes, so you need a way to get a more convenient random number from it: public static int GetInt(RNGCryptoServiceProvider rnd, int max) { byte[] r = new byte[4]; int value; do { rnd.GetBytes(r); value = BitConverter.ToInt32(r, 0) & Int32.MaxValue; } ...
RNGCryptoServiceProvider is marked as obsolete, starting in . NET 6.
Provides cryptographic services, including secure encoding and decoding of data, as well as many other operations, such as hashing, random number generation, and message authentication.
System.Security.Cryptography.RandomNumberGenerator
is the base class for Cryptographically-Secure Pseudo-Random Number Generator (CSPRNG) implementations. In .NET Framework RandomNumberGenerator.Create()
returns an RNGCryptoServiceProvider
instance (unless configured differently by CryptoConfig
). In .NET Core RandomNumberGenerator.Create()
returns an opaque type which is based on BCryptGenRandom (Windows) or OpenSSL's random number generator (!Windows).
RandomNumberGenerator.Create()
is the only way to get an RNG instance on .NET Core, and since it works on both .NET Core and .NET Framework is the most portable.
Of course, if you're generating an IV, you can also just call the instance method SymmetricAlgorithm.GenerateIV()
to have it use the CSPRNG internally; though as the documentation says, calling it is unnecessary as a random IV is created with the instance (GenerateIV
can be used to force it to generate a new one before the next call to CreateEncryptor
).
One of the solutions suggested is to use RandomNumberGenerator.Create() https://github.com/dotnet/corefx/issues/2881
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With