## 정의

PBKDF1 알고리즘의 확장을 사용하여 암호에서 키를 파생시킵니다.Derives a key from a password using an extension of the PBKDF1 algorithm.

``public ref class PasswordDeriveBytes : System::Security::Cryptography::DeriveBytes``
``````[System.Runtime.InteropServices.ComVisible(true)]
``````type PasswordDeriveBytes = class
inherit DeriveBytes``````
``````Public Class PasswordDeriveBytes
Inherits DeriveBytes``````
상속
특성

## 예제

다음 코드 예제에서는 사용 하 여 암호에서 키를 만듭니다를 PasswordDeriveBytes 클래스입니다.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);
}
else
{
randomBytes = gcnew array <Byte>(1);
}

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

// Fill the buffer with random bytes.
cryptoRNGProvider->GetBytes(randomBytes);

// 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>^ randomSalt = CreateRandomSalt(7);

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

try
{

// Create a PasswordDeriveBytes object and then create
// a TripleDES key from the password and salt.

// Create the key and set it to the Key property
// of the TripleDESCryptoServiceProvider object.
("TripleDES", "SHA1", 192, cryptoDESProvider->IV);
Console::WriteLine("Operation complete.");
}
catch (Exception^ ex)
{
Console::WriteLine(ex->Message);
}
finally
{
// Clear the buffers
ClearBytes(randomSalt);

// Clear the key.
cryptoDESProvider->Clear();
}

}
``````
``````using System;
using System.Security.Cryptography;
using System.Text;

{

public static void Main(String[] args)
{

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

byte[] salt = CreateRandomSalt(7);

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

try
{

// Create a PasswordDeriveBytes object and then create
// a TripleDES key from the password and 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();
}

}

//////////////////////////////////////////////////////////
// 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; x++)
{
buffer[x] = 0;
}
}
}
``````
``````Imports System.Security.Cryptography
Imports System.Text

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

' 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.
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

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 RNGCryptoServiceProvider.
Dim rand As New RNGCryptoServiceProvider()

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

' 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
``````

## 설명

이 클래스는 암호에서 키 자료로 사용 하기에 적합 한 바이트를 파생 시키는 PKCS #5 v2.0 표준에 정의 된 PBKDF1 알고리즘의 확장을 사용 합니다.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. 표준은 IETF RRC 2898에 설명 되어 있습니다.The standard is documented in IETF RRC 2898.

중요

하드 코드 하지 소스 코드 내에서 암호입니다.Never hard-code a password within your source code. 사용 하 여 어셈블리에서 하드 코드 된 암호를 검색할 수는 Ildasm.exe (IL 디스어셈블러) 도구, 편집기를 16 진수 또는 notepad.exe와 같은 텍스트 편집기에서 어셈블리를 여는 합니다.Hard coded passwords can be retrieved from an assembly using the Ildasm.exe (IL Disassembler) tool, a hex editor, or by simply opening up the assembly in a text editor like notepad.exe.