PasswordDeriveBytes Construtores
Definição
Importante
Algumas informações se referem a produtos de pré-lançamento que podem ser substancialmente modificados antes do lançamento. A Microsoft não oferece garantias, expressas ou implícitas, das informações aqui fornecidas.
Inicializa uma nova instância da classe PasswordDeriveBytes.
Sobrecargas
| Nome | Description |
|---|---|
| PasswordDeriveBytes(Byte[], Byte[]) |
Inicializa uma nova instância da PasswordDeriveBytes classe especificando a senha e o sal de chaves a serem usados para derivar a chave. |
| PasswordDeriveBytes(String, Byte[]) |
Inicializa uma nova instância da PasswordDeriveBytes classe com a senha e o sal de chave a serem usados para derivar a chave. |
| PasswordDeriveBytes(Byte[], Byte[], CspParameters) |
Inicializa uma nova instância da PasswordDeriveBytes classe que especifica a senha, o sal de chaves e o CSP (provedor de serviços criptográficos) a serem usados para derivar a chave. |
| PasswordDeriveBytes(String, Byte[], CspParameters) |
Inicializa uma nova instância da PasswordDeriveBytes classe com os parâmetros de senha, sal de chave e CSP (provedor de serviços criptográficos) a serem usados para derivar a chave. |
| PasswordDeriveBytes(Byte[], Byte[], String, Int32) |
Inicializa uma nova instância da PasswordDeriveBytes classe especificando a senha, o sal da chave, o nome do hash e as iterações a serem usadas para derivar a chave. |
| PasswordDeriveBytes(String, Byte[], String, Int32) |
Inicializa uma nova instância da PasswordDeriveBytes classe com a senha, o sal da chave, o nome do hash e o número de iterações a serem usadas para derivar a chave. |
| PasswordDeriveBytes(Byte[], Byte[], String, Int32, CspParameters) |
Inicializa uma nova instância da PasswordDeriveBytes classe que especifica a senha, o sal da chave, o nome do hash, as iterações e o CSP (provedor de serviços criptográficos) a serem usados para derivar a chave. |
| PasswordDeriveBytes(String, Byte[], String, Int32, CspParameters) |
Inicializa uma nova instância da PasswordDeriveBytes classe com a senha, o sal da chave, o nome do hash, o número de iterações e os parâmetros do CSP (provedor de serviços criptográficos) a serem usados para derivar a chave. |
PasswordDeriveBytes(Byte[], Byte[])
Inicializa uma nova instância da PasswordDeriveBytes classe especificando a senha e o sal de chaves a serem usados para derivar a chave.
public:
PasswordDeriveBytes(cli::array <System::Byte> ^ password, cli::array <System::Byte> ^ salt);
public PasswordDeriveBytes(byte[] password, byte[] salt);
new System.Security.Cryptography.PasswordDeriveBytes : byte[] * byte[] -> System.Security.Cryptography.PasswordDeriveBytes
Public Sub New (password As Byte(), salt As Byte())
Parâmetros
- password
- Byte[]
A senha para a qual derivar a chave.
- salt
- Byte[]
O sal de chave a ser usado para derivar a chave.
Exemplos
O exemplo de código a seguir cria uma chave de uma senha usando a PasswordDeriveBytes classe.
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
Comentários
Importante
Nunca codifique uma senha no código-fonte. Senhas codificadas em código podem ser recuperadas de um assembly usando o Ildasm.exe (IL Disassembler), um editor hex ou simplesmente abrindo o assembly em um editor de texto, como Notepad.exe.
Confira também
Aplica-se a
PasswordDeriveBytes(String, Byte[])
Inicializa uma nova instância da PasswordDeriveBytes classe com a senha e o sal de chave a serem usados para derivar a chave.
public:
PasswordDeriveBytes(System::String ^ strPassword, cli::array <System::Byte> ^ rgbSalt);
public PasswordDeriveBytes(string strPassword, byte[] rgbSalt);
new System.Security.Cryptography.PasswordDeriveBytes : string * byte[] -> System.Security.Cryptography.PasswordDeriveBytes
Public Sub New (strPassword As String, rgbSalt As Byte())
Parâmetros
- strPassword
- String
A senha para a qual derivar a chave.
- rgbSalt
- Byte[]
O sal de chave a ser usado para derivar a chave.
Comentários
Importante
Nunca codifique uma senha no código-fonte. Senhas codificadas em código podem ser recuperadas de um assembly usando a ferramenta Ildasm.exe (IL Disassembler), um editor hex ou simplesmente abrindo o assembly em um editor de texto como notepad.exe.
Confira também
Aplica-se a
PasswordDeriveBytes(Byte[], Byte[], CspParameters)
Inicializa uma nova instância da PasswordDeriveBytes classe que especifica a senha, o sal de chaves e o CSP (provedor de serviços criptográficos) a serem usados para derivar a chave.
public:
PasswordDeriveBytes(cli::array <System::Byte> ^ password, cli::array <System::Byte> ^ salt, System::Security::Cryptography::CspParameters ^ cspParams);
public PasswordDeriveBytes(byte[] password, byte[] salt, System.Security.Cryptography.CspParameters cspParams);
new System.Security.Cryptography.PasswordDeriveBytes : byte[] * byte[] * System.Security.Cryptography.CspParameters -> System.Security.Cryptography.PasswordDeriveBytes
Public Sub New (password As Byte(), salt As Byte(), cspParams As CspParameters)
Parâmetros
- password
- Byte[]
A senha para a qual derivar a chave.
- salt
- Byte[]
O sal de chave a ser usado para derivar a chave.
- cspParams
- CspParameters
Os parâmetros do CSP (provedor de serviços criptográficos) para a operação.
Exemplos
O exemplo de código a seguir cria uma chave de uma senha usando a PasswordDeriveBytes classe.
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
Comentários
Importante
Nunca codifique uma senha no código-fonte. Senhas codificadas em código podem ser recuperadas de um assembly usando o Ildasm.exe (IL Disassembler) l, um editor hex ou simplesmente abrindo o assembly em um editor de texto, como Notepad.exe.
Confira também
Aplica-se a
PasswordDeriveBytes(String, Byte[], CspParameters)
Inicializa uma nova instância da PasswordDeriveBytes classe com os parâmetros de senha, sal de chave e CSP (provedor de serviços criptográficos) a serem usados para derivar a chave.
public:
PasswordDeriveBytes(System::String ^ strPassword, cli::array <System::Byte> ^ rgbSalt, System::Security::Cryptography::CspParameters ^ cspParams);
public PasswordDeriveBytes(string strPassword, byte[] rgbSalt, System.Security.Cryptography.CspParameters cspParams);
new System.Security.Cryptography.PasswordDeriveBytes : string * byte[] * System.Security.Cryptography.CspParameters -> System.Security.Cryptography.PasswordDeriveBytes
Public Sub New (strPassword As String, rgbSalt As Byte(), cspParams As CspParameters)
Parâmetros
- strPassword
- String
A senha para a qual derivar a chave.
- rgbSalt
- Byte[]
O sal de chave a ser usado para derivar a chave.
- cspParams
- CspParameters
Os parâmetros CSP para a operação.
Comentários
Importante
Nunca codifique uma senha no código-fonte. Senhas codificadas em código podem ser recuperadas de um assembly usando a ferramenta Ildasm.exe (IL Disassembler), um editor hex ou simplesmente abrindo o assembly em um editor de texto como notepad.exe.
Confira também
Aplica-se a
PasswordDeriveBytes(Byte[], Byte[], String, Int32)
Inicializa uma nova instância da PasswordDeriveBytes classe especificando a senha, o sal da chave, o nome do hash e as iterações a serem usadas para derivar a chave.
public:
PasswordDeriveBytes(cli::array <System::Byte> ^ password, cli::array <System::Byte> ^ salt, System::String ^ hashName, int iterations);
public PasswordDeriveBytes(byte[] password, byte[] salt, string hashName, int iterations);
new System.Security.Cryptography.PasswordDeriveBytes : byte[] * byte[] * string * int -> System.Security.Cryptography.PasswordDeriveBytes
Public Sub New (password As Byte(), salt As Byte(), hashName As String, iterations As Integer)
Parâmetros
- password
- Byte[]
A senha para a qual derivar a chave.
- salt
- Byte[]
O sal de chave a ser usado para derivar a chave.
- hashName
- String
O algoritmo de hash a ser usado para derivar a chave.
- iterations
- Int32
A contagem de iteração a ser usada para derivar a chave.
Comentários
Importante
Nunca codifique uma senha no código-fonte. Senhas codificadas em código podem ser recuperadas de um assembly usando o Ildasm.exe (IL Disassembler), um editor hex ou simplesmente abrindo o assembly em um editor de texto, como Notepad.exe.
Para obter uma lista de nomes de algoritmo de hash, consulte a CryptoConfig classe.
Confira também
Aplica-se a
PasswordDeriveBytes(String, Byte[], String, Int32)
Inicializa uma nova instância da PasswordDeriveBytes classe com a senha, o sal da chave, o nome do hash e o número de iterações a serem usadas para derivar a chave.
public:
PasswordDeriveBytes(System::String ^ strPassword, cli::array <System::Byte> ^ rgbSalt, System::String ^ strHashName, int iterations);
public PasswordDeriveBytes(string strPassword, byte[] rgbSalt, string strHashName, int iterations);
new System.Security.Cryptography.PasswordDeriveBytes : string * byte[] * string * int -> System.Security.Cryptography.PasswordDeriveBytes
Public Sub New (strPassword As String, rgbSalt As Byte(), strHashName As String, iterations As Integer)
Parâmetros
- strPassword
- String
A senha para a qual derivar a chave.
- rgbSalt
- Byte[]
O sal de chave a ser usado para derivar a chave.
- strHashName
- String
O nome do algoritmo de hash para a operação.
- iterations
- Int32
O número de iterações para a operação.
Comentários
Importante
Nunca codifique uma senha no código-fonte. Senhas codificadas em código podem ser recuperadas de um assembly usando a ferramenta Ildasm.exe (IL Disassembler), um editor hex ou simplesmente abrindo o assembly em um editor de texto como notepad.exe.
Para obter uma lista de nomes de algoritmo de hash, consulte a CryptoConfig classe.
Confira também
Aplica-se a
PasswordDeriveBytes(Byte[], Byte[], String, Int32, CspParameters)
Inicializa uma nova instância da PasswordDeriveBytes classe que especifica a senha, o sal da chave, o nome do hash, as iterações e o CSP (provedor de serviços criptográficos) a serem usados para derivar a chave.
public:
PasswordDeriveBytes(cli::array <System::Byte> ^ password, cli::array <System::Byte> ^ salt, System::String ^ hashName, int iterations, System::Security::Cryptography::CspParameters ^ cspParams);
public PasswordDeriveBytes(byte[] password, byte[] salt, string hashName, int iterations, System.Security.Cryptography.CspParameters cspParams);
new System.Security.Cryptography.PasswordDeriveBytes : byte[] * byte[] * string * int * System.Security.Cryptography.CspParameters -> System.Security.Cryptography.PasswordDeriveBytes
Public Sub New (password As Byte(), salt As Byte(), hashName As String, iterations As Integer, cspParams As CspParameters)
Parâmetros
- password
- Byte[]
A senha para a qual derivar a chave.
- salt
- Byte[]
O sal de chave a ser usado para derivar a chave.
- hashName
- String
O algoritmo de hash a ser usado para derivar a chave.
- iterations
- Int32
A contagem de iteração a ser usada para derivar a chave.
- cspParams
- CspParameters
Os parâmetros do CSP (provedor de serviços criptográficos) para a operação.
Comentários
Importante
Nunca codifique uma senha no código-fonte. Senhas codificadas em código podem ser recuperadas de um assembly usando o Ildasm.exe (IL Disassembler), um editor hex ou simplesmente abrindo o assembly em um editor de texto, como Notepad.exe.
Para obter uma lista de nomes de algoritmo de hash, consulte a CryptoConfig classe.
Confira também
Aplica-se a
PasswordDeriveBytes(String, Byte[], String, Int32, CspParameters)
Inicializa uma nova instância da PasswordDeriveBytes classe com a senha, o sal da chave, o nome do hash, o número de iterações e os parâmetros do CSP (provedor de serviços criptográficos) a serem usados para derivar a chave.
public:
PasswordDeriveBytes(System::String ^ strPassword, cli::array <System::Byte> ^ rgbSalt, System::String ^ strHashName, int iterations, System::Security::Cryptography::CspParameters ^ cspParams);
public PasswordDeriveBytes(string strPassword, byte[] rgbSalt, string strHashName, int iterations, System.Security.Cryptography.CspParameters cspParams);
new System.Security.Cryptography.PasswordDeriveBytes : string * byte[] * string * int * System.Security.Cryptography.CspParameters -> System.Security.Cryptography.PasswordDeriveBytes
Public Sub New (strPassword As String, rgbSalt As Byte(), strHashName As String, iterations As Integer, cspParams As CspParameters)
Parâmetros
- strPassword
- String
A senha para a qual derivar a chave.
- rgbSalt
- Byte[]
O sal de chave a ser usado para derivar a chave.
- strHashName
- String
O nome do algoritmo de hash para a operação.
- iterations
- Int32
O número de iterações para a operação.
- cspParams
- CspParameters
Os parâmetros CSP para a operação.
Comentários
Importante
Nunca codifique uma senha no código-fonte. Senhas codificadas em código podem ser recuperadas de um assembly usando a ferramenta Ildasm.exe (IL Disassembler), um editor hex ou simplesmente abrindo o assembly em um editor de texto como notepad.exe.
Para obter uma lista de nomes de algoritmo de hash, consulte a CryptoConfig classe.