Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

RNGCryptoServiceProvider Class

Implements a cryptographic Random Number Generator (RNG) using the implementation provided by the cryptographic service provider (CSP). This class cannot be inherited.

Namespace: System.Security.Cryptography
Assembly: mscorlib (in mscorlib.dll)

'Declaration
<ComVisibleAttribute(True)> _
Public NotInheritable Class RNGCryptoServiceProvider
	Inherits RandomNumberGenerator
'Usage
Dim instance As RNGCryptoServiceProvider

/** @attribute ComVisibleAttribute(true) */ 
public final class RNGCryptoServiceProvider extends RandomNumberGenerator
ComVisibleAttribute(true) 
public final class RNGCryptoServiceProvider extends RandomNumberGenerator
Not applicable.

The following code example shows how to create a random number with the RNGCryptoServiceProvider class.

'The following sample uses the Cryptography class to simulate the roll of a dice.
Imports System
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography



Class RNGCSP
    
    ' Main method.
    Public Shared Sub Main()
        ' Roll the dice 30 times and display 
        ' the results to the console.
        Dim x As Integer
        For x = 0 To 30
            Console.WriteLine(RollDice(6))
        Next x
    End Sub 'Main
     
    ' This method simulates a roll of the dice. The input parameter is the 
    ' number of sides of the dice.
    Public Shared Function RollDice(NumSides As Integer) As Integer
        ' Create a byte array to hold the random value.
        Dim randomNumber(0) As Byte
        
        ' Create a new instance of the RNGCryptoServiceProvider. 
        Dim Gen As New RNGCryptoServiceProvider()
        
        ' Fill the array with a random value.
        Gen.GetBytes(randomNumber)
        
        ' Convert the byte to an integer value to make the modulus operation easier.
        Dim rand As Integer = Convert.ToInt32(randomNumber(0))
        
        ' Return the random number mod the number
        ' of sides.  The possible values are zero-
        ' based, so we add one.
        Return rand Mod NumSides + 1
    End Function 'RollDice
End Class 'CryptoMemoryStream


System.Object
   System.Security.Cryptography.RandomNumberGenerator
    System.Security.Cryptography.RNGCryptoServiceProvider

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions

Show:
© 2017 Microsoft