locked
RNGCryptoServiceProvider in .Net Core RRS feed

  • Question

  • HI i am searching for highly Cryptographically secure random values, i need to use in .net core 2.2 framework

    in microsoft saying  below two RNG's present in windows, i tried  using RNGCryptoServiceProvider it worked well.

    1. Using RNGCryptoServiceProvider[edit]
    Programmers using .NET should use the RNGCryptoServiceProvider 

    2. Using Cryptography API: Next Generation (CNG)  is a long term replacement for the deprecated Crypto API. It provides an equivalent function BCryptGenRandom as well as dedicated functions for key generation.

    Query Does RNGCryptoServiceProvider has its own algorithm in .net core or it uses BCryptGenRandom C++ calls internally ? 


    Wednesday, April 8, 2020 1:41 PM

Answers

  • Hello DangerousBaby,

    >>>Query Does RNGCryptoServiceProvider has its own algorithm in .net core or it uses BCryptGenRandom C++ calls internally ? 

    Yes, it does call BCryptGenRandom.

    RNGCryptoServiceProvider via RandomNumberGenerator to generate random values. You can find implementation of RandomNumberGenerator on Windows at

    namespace System.Security.Cryptography
    {
        internal partial class RandomNumberGeneratorImplementation
        {
            private static unsafe void GetBytes(byte* pbBuffer, int count)
            {
                Debug.Assert(count > 0);
    
                Interop.BCrypt.NTSTATUS status = Interop.BCrypt.BCryptGenRandom(IntPtr.Zero, pbBuffer, count, Interop.BCrypt.BCRYPT_USE_SYSTEM_PREFERRED_RNG);
                if (status != Interop.BCrypt.NTSTATUS.STATUS_SUCCESS)
                    throw Interop.BCrypt.CreateCryptographicException(status);
            }
        }
    }

    Please let me know if it helps.

    Best regards,

    Rita

     

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 9, 2020 3:11 AM

All replies

  • Hello DangerousBaby,

    >>>Query Does RNGCryptoServiceProvider has its own algorithm in .net core or it uses BCryptGenRandom C++ calls internally ? 

    Yes, it does call BCryptGenRandom.

    RNGCryptoServiceProvider via RandomNumberGenerator to generate random values. You can find implementation of RandomNumberGenerator on Windows at

    namespace System.Security.Cryptography
    {
        internal partial class RandomNumberGeneratorImplementation
        {
            private static unsafe void GetBytes(byte* pbBuffer, int count)
            {
                Debug.Assert(count > 0);
    
                Interop.BCrypt.NTSTATUS status = Interop.BCrypt.BCryptGenRandom(IntPtr.Zero, pbBuffer, count, Interop.BCrypt.BCRYPT_USE_SYSTEM_PREFERRED_RNG);
                if (status != Interop.BCrypt.NTSTATUS.STATUS_SUCCESS)
                    throw Interop.BCrypt.CreateCryptographicException(status);
            }
        }
    }

    Please let me know if it helps.

    Best regards,

    Rita

     

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 9, 2020 3:11 AM
  • Thank you so for your quick response.. Lovely !

    microsoft fourm

    Sunday, April 26, 2020 1:04 PM