PasswordDeriveBytes Constructor (Byte[], Byte[])

PasswordDeriveBytes Constructor (Byte[], Byte[])

Note: This constructor is new in the .NET Framework version 2.0.

Initializes a new instance of the PasswordDeriveBytes class specifying the password and key salt to use to derive the key.

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

PasswordDeriveBytes (
	array<unsigned char>^ password, 
	array<unsigned char>^ salt
public PasswordDeriveBytes (
	byte[] password, 
	byte[] salt
public function PasswordDeriveBytes (
	password : byte[], 
	salt : byte[]



The password to derive the key for.


The key salt to use to derive the key.

Security noteSecurity Note

Never hard code a password within your source code. Hard-coded passwords can be retrieved from an assembly using the MSIL Disassembler (Ildasm.exe), a hex editor, or by simply opening up the assembly in a text editor such as Notepad.exe.

The following code example creates a key from a password using the PasswordDeriveBytes class.

using namespace System;
using namespace System::Security::Cryptography;
using namespace System::Text;

// Generates a random salt value of the specified length.
array<Byte>^ CreateRandomSalt(int length)
    // Create a buffer
    array<Byte>^ randomBytes;

    if (length >= 1)
        randomBytes = gcnew array <Byte>(length);
        randomBytes = gcnew array <Byte>(1);

    // Create a new RNGCryptoServiceProvider.
    RNGCryptoServiceProvider^ cryptoRNGProvider =
        gcnew RNGCryptoServiceProvider();

    // Fill the buffer with random bytes.

    // return the bytes.
    return randomBytes;

// Clears the bytes in a buffer so they can't later be read from memory.
void ClearBytes(array<Byte>^ buffer)
    // Check arguments.
    if (buffer == nullptr)
        throw gcnew ArgumentNullException("buffer");

    // Set each byte in the buffer to 0.
    for (int x = 0; x <= buffer->Length - 1; x++)
        buffer[x] = 0;

int main(array<String^>^ args)

    // Get a password from the user.
    Console::WriteLine("Enter a password to produce a key:");

    // Security Note: Never hard-code a password within your
    // source code.  Hard-coded passwords can be retrieved
    // from a compiled assembly.
    array<Byte>^ password = Encoding::Unicode->GetBytes(Console::ReadLine());

    array<Byte>^ randomSalt = CreateRandomSalt(7);

    // Create a TripleDESCryptoServiceProvider object.
    TripleDESCryptoServiceProvider^ cryptoDESProvider = 
        gcnew TripleDESCryptoServiceProvider();

        Console::WriteLine("Creating a key with PasswordDeriveBytes...");

        // Create a PasswordDeriveBytes object and then create
        // a TripleDES key from the password and salt.
        PasswordDeriveBytes^ passwordDeriveBytes = gcnew PasswordDeriveBytes
            (password->ToString(), randomSalt);

        // Create the key and add it to the Key property.
        cryptoDESProvider->Key = passwordDeriveBytes->CryptDeriveKey
            ("TripleDES", "SHA1", 192, cryptoDESProvider->IV);

        Console::WriteLine("Operation complete.");
    catch (Exception^ ex)
        // Clear the buffers

        // Clear the key.


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

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

.NET Framework

Supported in: 2.0

Community Additions

© 2015 Microsoft