Export (0) Print
Expand All

PasswordDeriveBytes Constructor (Byte[], Byte[], CspParameters)

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

Initializes a new instance of the PasswordDeriveBytes class specifying the password, key salt, and cryptographic service provider (CSP) to use to derive the key.

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

public PasswordDeriveBytes (
	byte[] password,
	byte[] salt,
	CspParameters cspParams
)
public PasswordDeriveBytes (
	byte[] password, 
	byte[] salt, 
	CspParameters cspParams
)
public function PasswordDeriveBytes (
	password : byte[], 
	salt : byte[], 
	cspParams : CspParameters
)

Parameters

password

The password to derive the key for.

salt

The key salt to use to derive the key.

cspParams

The cryptographic service provider (CSP) parameters for the operation.

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) l, 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 System;
using System.Security.Cryptography;
using System.Text;

public class PasswordDerivedBytesExample
{

    public static void Main(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.
        //********************************************************

        byte[] pwd = Encoding.Unicode.GetBytes(Console.ReadLine());

        byte[] salt = createRandomSalt(7);

        // Create a TripleDESCryptoServiceProvider object.
        TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();

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

            // Create a PasswordDeriveBytes object and then create 
            // a TripleDES key from the password and salt.
            PasswordDeriveBytes pdb = new PasswordDeriveBytes(pwd, salt);

            // Create the key and add it to the Key property.
            tdes.Key = pdb.CryptDeriveKey("TripleDES", "SHA1", 192, tdes.IV);

            Console.WriteLine("Operation complete.");
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        finally
        {
            // Clear the buffers
            clearBytes(pwd);
            clearBytes(salt);

            // Clear the key.
            tdes.Clear();
        }

        Console.ReadLine();
    }

    //********************************************************
    //* Helper methods:
    //* createRandomSalt: Generates a random salt value of the 
    //*                   specified length.  
    //*
    //* clearBytes: Clear the bytes in a buffer so they can't 
    //*             later be read from memory.
    //********************************************************

    public static byte[] createRandomSalt(int Length)
    {
        // Create a buffer
        byte[] randBytes;

        if (Length >= 1)
        {
            randBytes = new byte[Length];
        }
        else
        {
            randBytes = new byte[1];
        }

        // Create a new RNGCryptoServiceProvider.
        RNGCryptoServiceProvider rand = new RNGCryptoServiceProvider();

        // Fill the buffer with random bytes.
        rand.GetBytes(randBytes);

        // return the bytes.
        return randBytes;
    }

    public static void clearBytes(byte[] Buffer)
    {
        // Check arguments.
        if (Buffer == null)
        {
            throw new ArgumentException("Buffer");
        }

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

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

ADD
Show:
© 2014 Microsoft