This documentation is archived and is not being maintained.

Rfc2898DeriveBytes::GetBytes Method

Updated: December 2008

Returns the pseudo-random key for this object.

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

virtual array<unsigned char>^ GetBytes(
	int cb
) override


Type: System::Int32

The number of pseudo-random key bytes to generate.

Return Value

Type: array<System::Byte>
A byte array filled with pseudo-random key bytes.


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.

		 Rfc2898DeriveBytes ^ k1 = gcnew Rfc2898DeriveBytes( pwd1,salt1,myIterations );
		 Rfc2898DeriveBytes ^ k2 = gcnew Rfc2898DeriveBytes( pwd1,salt1 );

		 // Encrypt the data.
		 TripleDES^ encAlg = TripleDES::Create();
		 encAlg->Key = k1->GetBytes( 16 );
		 MemoryStream^ encryptionStream = gcnew MemoryStream;
		 CryptoStream^ encrypt = gcnew CryptoStream( encryptionStream,encAlg->CreateEncryptor(),CryptoStreamMode::Write );
		 array<Byte>^utfD1 = (gcnew 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




December 2008

Corrected example introduction.

Customer feedback.