Export (0) Print
Expand All
1 out of 2 rated this helpful - Rate this topic

PasswordDeriveBytes Class

Derives a key from a password using an extension of the PBKDF1 algorithm.

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

[ComVisibleAttribute(true)] 
public class PasswordDeriveBytes : DeriveBytes
/** @attribute ComVisibleAttribute(true) */ 
public class PasswordDeriveBytes extends DeriveBytes
ComVisibleAttribute(true) 
public class PasswordDeriveBytes extends DeriveBytes

This class uses an extension of the PBKDF1 algorithm defined in the PKCS#5 v2.0 standard to derive bytes suitable for use as key material from a password. The standard is documented in IETF RRC 2898.

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) tool, a hex editor, or by simply opening up the assembly in a text editor like 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;
        }
    }
}

System.Object
   System.Security.Cryptography.DeriveBytes
    System.Security.Cryptography.PasswordDeriveBytes
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

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, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.