Export (0) Print
Expand All

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

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)

public PasswordDeriveBytes (
	byte[] password,
	byte[] salt
)
public PasswordDeriveBytes (
	byte[] password, 
	byte[] salt
)
public function PasswordDeriveBytes (
	password : byte[], 
	salt : byte[]
)
Not applicable.

Parameters

password

The password to derive the key for.

salt

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 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:");

        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 set it to the Key property
            // of the TripleDESCryptoServiceProvider object.
            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 Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft