Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

RNGCryptoServiceProvider.GetBytes Method

Fills an array of bytes with a cryptographically strong random sequence of values.

[Visual Basic]
Overrides Public Sub GetBytes( _
   ByVal data() As Byte _
)
[C#]
public override void GetBytes(
 byte[] data
);
[C++]
public: void GetBytes(
 unsigned char data __gc[]
);
[JScript]
public override function GetBytes(
   data : Byte[]
);

Parameters

data
The array to fill with cryptographically strong random bytes.

Exceptions

Exception Type Condition
CryptographicException The cryptographic service provider (CSP) cannot be acquired.

Remarks

The length of the byte array determines how many cryptographically strong random bytes are produced.

Example

[Visual Basic, C#, C++] The following example creates a random sequence 100 bytes long and stores it in random.

[Visual Basic] 
Dim random() As Byte = New Byte(100) {}
       
'RNGCryptoServiceProvider is an implementation of an RNG
Dim rng As New RNGCryptoServiceProvider()
rng.GetBytes(random) ' bytes in random are now random

[C#] 
byte[] random = new Byte[100];

//RNGCryptoServiceProvider is an implementation of a random number generator.
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetBytes(random); // The array is now filled with cryptographically strong random bytes.

[C++] 
Byte random[] = new Byte[100];

//RNGCryptoServiceProvider is an implementation of a random number generator.
RNGCryptoServiceProvider* rng = new RNGCryptoServiceProvider();
rng->GetBytes(random); // The array is now filled with cryptographically strong random bytes.

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

RNGCryptoServiceProvider Class | RNGCryptoServiceProvider Members | System.Security.Cryptography Namespace | Cryptographic Services

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.