Export (0) Print
Expand All

Rfc2898DeriveBytes.GetBytes Method

Updated: December 2008

Returns the pseudo-random key for this object.

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

'Declaration
Public Overrides Function GetBytes ( _
	cb As Integer _
) As Byte()
'Usage
Dim instance As Rfc2898DeriveBytes 
Dim cb As Integer 
Dim returnValue As Byte()

returnValue = instance.GetBytes(cb)

Parameters

cb
Type: System.Int32

The number of pseudo-random key bytes to generate.

Return Value

Type: System.Byte()
A byte array filled with pseudo-random key bytes.

ExceptionCondition
ArgumentOutOfRangeException

cb is out of range. This parameter requires a non-negative number.

The Rfc2898DeriveBytes class implements PBKDF2 functionality by using a pseudorandom number generator based on HMACSHA1. The Rfc2898DeriveBytes class takes a password, a salt, and an iteration count, and then generates keys through calls to the GetBytes method. Repeated calls to this method will not generate the same key; instead, appending two calls of the GetBytes method with a cb parameter value of 20 is the equivalent of calling the GetBytes method once with a cb parameter value of 40.

The following example shows how to use the GetBytes method to get the key for an instance of Rfc2898DeriveBytes. This code example is part of a larger example provided for the Rfc2898DeriveBytes class.

Try 
    Dim k1 As New Rfc2898DeriveBytes(pwd1, salt1, myIterations)
    Dim k2 As New Rfc2898DeriveBytes(pwd1, salt1)
    ' Encrypt the data. 
    Dim encAlg As TripleDES = TripleDES.Create()
    encAlg.Key = k1.GetBytes(16)
    Dim encryptionStream As New MemoryStream()
    Dim encrypt As New CryptoStream(encryptionStream, encAlg.CreateEncryptor(), CryptoStreamMode.Write)
    Dim utfD1 As Byte() = New System.Text.UTF8Encoding(False).GetBytes(data1)

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

Date

History

Reason

December 2008

Corrected example introduction.

Customer feedback.

Community Additions

ADD
Show:
© 2014 Microsoft