public static void BCryptGenRandom(byte[] buffer) { const int BCRYPT_USE_SYSTEM_PREFERRED_RNG = 0x00000002; NTSTATUS ntStatus = Interop.BCryptGenRandom(IntPtr.Zero, buffer, buffer.Length, BCRYPT_USE_SYSTEM_PREFERRED_RNG); if (ntStatus != NTSTATUS.STATUS_SUCCESS) { throw CreateCryptographicException(ntStatus); } }