PasswordDeriveBytes Osztály

Definíció

Egy jelszót a PBKDF1 algoritmus bővítményével nyer ki.

public ref class PasswordDeriveBytes : System::Security::Cryptography::DeriveBytes
public class PasswordDeriveBytes : System.Security.Cryptography.DeriveBytes
[System.Runtime.InteropServices.ComVisible(true)]
public class PasswordDeriveBytes : System.Security.Cryptography.DeriveBytes
type PasswordDeriveBytes = class
    inherit DeriveBytes
[<System.Runtime.InteropServices.ComVisible(true)>]
type PasswordDeriveBytes = class
    inherit DeriveBytes
Public Class PasswordDeriveBytes
Inherits DeriveBytes
Öröklődés
PasswordDeriveBytes
Attribútumok

Példák

Az alábbi példakód egy kulcsot hoz létre egy jelszóból az PasswordDeriveBytes osztály használatával.

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.
            // This example uses the SHA1 algorithm.
            // Due to collision problems with SHA1, Microsoft recommends SHA256 or better.
            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];
        }

        using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
        {
            // Fill the buffer with random bytes.
            rng.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; x++)
        {
            buffer[x] = 0;
        }
    }
}
Imports System.Security.Cryptography
Imports System.Text



Module PasswordDerivedBytesExample


    Sub Main(ByVal args() As String)

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

        Dim pwd As Byte() = Encoding.Unicode.GetBytes(Console.ReadLine())

        Dim salt As Byte() = CreateRandomSalt(7)

        ' Create a TripleDESCryptoServiceProvider object.
        Dim tdes As 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.
            Dim pdb As New PasswordDeriveBytes(pwd, salt)


            ' Create the key and set it to the Key property
            ' of the TripleDESCryptoServiceProvider object.
            ' This example uses the SHA1 algorithm.
            ' Due to collision problems with SHA1, Microsoft recommends SHA256 or better.
            tdes.Key = pdb.CryptDeriveKey("TripleDES", "SHA1", 192, tdes.IV)


            Console.WriteLine("Operation complete.")
        Catch e As Exception
            Console.WriteLine(e.Message)
        Finally
            ' Clear the buffers
            ClearBytes(pwd)
            ClearBytes(salt)

            ' Clear the key.
            tdes.Clear()
        End Try

        Console.ReadLine()

    End Sub


    '********************************************************
    '* 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.
    '********************************************************
    Function CreateRandomSalt(ByVal length As Integer) As Byte()
        ' Create a buffer
        Dim randBytes() As Byte

        If length >= 1 Then
            randBytes = New Byte(length) {}
        Else
            randBytes = New Byte(0) {}
        End If

        ' Create a new RandomNumberGenerator.
        Using rand As RandomNumberGenerator = RandomNumberGenerator.Create()
            ' Fill the buffer with random bytes.
            rand.GetBytes(randBytes)
        End Using

        ' return the bytes.
        Return randBytes

    End Function


    Sub ClearBytes(ByVal buffer() As Byte)
        ' Check arguments.
        If buffer Is Nothing Then
            Throw New ArgumentException("buffer")
        End If

        ' Set each byte in the buffer to 0.
        Dim x As Integer
        For x = 0 To buffer.Length - 1
            buffer(x) = 0
        Next x

    End Sub
End Module

Megjegyzések

Ez az osztály a PKCS#5 v2.0 szabványban meghatározott PBKDF1 algoritmus bővítményét használja a jelszóból kulcsanyagként használható bájtok kinyerésére. A szabvány az IETF RRC 2898-ban van dokumentálva.

Important

Soha ne kódjon jelszót a forráskódon belül. A kemény kóddal kódolt jelszavak lekérhetők egy szerelvényből a Ildasm.exe (IL Disassembler) eszközzel, egy hexaszerkesztővel, vagy egyszerűen megnyitva a szerelvényt egy szövegszerkesztőben, például notepad.exe.

Konstruktorok

Name Description
PasswordDeriveBytes(Byte[], Byte[], CspParameters)

Inicializálja az PasswordDeriveBytes osztály új példányát, amely megadja a kulcs származtatásához használt jelszót, kulcssót és titkosítási szolgáltatót (CSP).

PasswordDeriveBytes(Byte[], Byte[], String, Int32, CspParameters)

Inicializálja az PasswordDeriveBytes osztály új példányát, amely megadja a kulcs származtatásához használt jelszót, kulcssót, kivonatnevet, iterációkat és titkosítási szolgáltatót (CSP).

PasswordDeriveBytes(Byte[], Byte[], String, Int32)

Inicializálja az PasswordDeriveBytes osztály új példányát, amely megadja a kulcs származtatásához használandó jelszót, kulcs sót, kivonatnevet és iterációkat.

PasswordDeriveBytes(Byte[], Byte[])

Inicializálja az PasswordDeriveBytes osztály új példányát, amely megadja a kulcs származtatásához használni kívánt jelszót és kulcssót.

PasswordDeriveBytes(String, Byte[], CspParameters)

Inicializálja az PasswordDeriveBytes osztály új példányát a kulcs származtatásához használandó jelszó, kulcssó és titkosítási szolgáltató (CSP) paraméterekkel.

PasswordDeriveBytes(String, Byte[], String, Int32, CspParameters)

Inicializálja az osztály új példányát a PasswordDeriveBytes kulcs származtatásához használandó jelszóval, kulcssóval, kivonatnévvel, iterációk számával és titkosítási szolgáltatói (CSP) paraméterekkel.

PasswordDeriveBytes(String, Byte[], String, Int32)

Inicializálja az PasswordDeriveBytes osztály új példányát a kulcs származtatásához használandó jelszóval, kulcssóval, kivonat nevével és számával.

PasswordDeriveBytes(String, Byte[])

Inicializálja az PasswordDeriveBytes osztály új példányát a kulcs származtatásához használandó jelszóval és kulcssóval.

Tulajdonságok

Name Description
HashName

Lekéri vagy beállítja a művelet kivonatoló algoritmusának nevét.

IterationCount

Lekéri vagy beállítja a művelet iterációinak számát.

Salt

Lekéri vagy beállítja a művelet fő sóértékét.

Metódusok

Name Description
CryptDeriveKey(String, String, Int32, Byte[])

Titkosítási kulcsot hoz létre az PasswordDeriveBytes objektumból.

Dispose()

Ha egy származtatott osztályban felül van bírálva, az osztály aktuális példánya által használt összes erőforrást felszabadítja DeriveBytes .

(Öröklődés forrása DeriveBytes)
Dispose(Boolean)

Felszabadítja az osztály által PasswordDeriveBytes használt nem felügyelt erőforrásokat, és opcionálisan felszabadítja a felügyelt erőforrásokat.

Equals(Object)

Meghatározza, hogy a megadott objektum egyenlő-e az aktuális objektummal.

(Öröklődés forrása Object)
Finalize()

Lehetővé teszi az objektumok számára, hogy megpróbálják felszabadítani az erőforrásokat, és más tisztítási műveleteket hajtsanak végre, mielőtt a szemétgyűjtés visszanyeri azt.

GetBytes(Int32)
Elavult.

Pszeudo-véletlenszerű kulcs bájtjait adja vissza.

GetHashCode()

Ez az alapértelmezett kivonatoló függvény.

(Öröklődés forrása Object)
GetType()

Lekéri az Type aktuális példányt.

(Öröklődés forrása Object)
MemberwiseClone()

Az aktuális Objectpéldány sekély másolatát hozza létre.

(Öröklődés forrása Object)
Reset()

Alaphelyzetbe állítja a művelet állapotát.

ToString()

Az aktuális objektumot jelképező sztringet ad vissza.

(Öröklődés forrása Object)

A következőre érvényes:

Lásd még