PasswordDeriveBytesとは? わかりやすく解説

PasswordDeriveBytes クラス

PBKDF1 アルゴリズム拡張機能使用してパスワードからキー派生させます

名前空間: System.Security.Cryptography
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<ComVisibleAttribute(True)> _
Public Class PasswordDeriveBytes
    Inherits DeriveBytes
Dim instance As PasswordDeriveBytes
[ComVisibleAttribute(true)] 
public class PasswordDeriveBytes : DeriveBytes
[ComVisibleAttribute(true)] 
public ref class PasswordDeriveBytes : public
 DeriveBytes
/** @attribute ComVisibleAttribute(true) */ 
public class PasswordDeriveBytes extends DeriveBytes
ComVisibleAttribute(true) 
public class PasswordDeriveBytes extends
 DeriveBytes
解説解説
使用例使用例

PasswordDeriveBytes クラス使用してパスワードからキー作成するコード例次に示します

Imports System
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:")

        '********************************************************
        '* Security Note: Never hard-code a password within your
        '* source code.  Hard-coded passwords can be retrieved 
        '* from a compiled assembly.
        '********************************************************
        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 add it to the Key property.
            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 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
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;
        }
    }
}
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>^ password = Encoding::Unicode->GetBytes(Console::ReadLine());

    array<Byte>^ randomSalt = CreateRandomSalt(7);

    // Create a TripleDESCryptoServiceProvider object.
    TripleDESCryptoServiceProvider^ cryptoDESProvider = 
        gcnew 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^ passwordDeriveBytes = gcnew PasswordDeriveBytes
            (password->ToString(), randomSalt);

        // Create the key and add it to the Key property.
        cryptoDESProvider->Key = passwordDeriveBytes->CryptDeriveKey
            ("TripleDES", "SHA1", 192, cryptoDESProvider->IV);

        Console::WriteLine("Operation complete.");
    }
    catch (Exception^ ex)
    {
        Console::WriteLine(ex->Message);
    }
    finally
    {
        // Clear the buffers
        ClearBytes(password);
        ClearBytes(randomSalt);

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

    Console::ReadLine();
}
継承階層継承階層
System.Object
   System.Security.Cryptography.DeriveBytes
    System.Security.Cryptography.PasswordDeriveBytes
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
PasswordDeriveBytes メンバ
System.Security.Cryptography 名前空間
その他の技術情報
暗号サービス

PasswordDeriveBytes コンストラクタ (String, Byte[])

キー派生させるために使用するパスワードおよびキー salt指定して、PasswordDeriveBytes クラス新しインスタンス初期化します。

名前空間: System.Security.Cryptography
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Sub New ( _
    strPassword As String, _
    rgbSalt As Byte() _
)
Dim strPassword As String
Dim rgbSalt As Byte()

Dim instance As New PasswordDeriveBytes(strPassword,
 rgbSalt)
public PasswordDeriveBytes (
    string strPassword,
    byte[] rgbSalt
)
public:
PasswordDeriveBytes (
    String^ strPassword, 
    array<unsigned char>^ rgbSalt
)
public PasswordDeriveBytes (
    String strPassword, 
    byte[] rgbSalt
)
public function PasswordDeriveBytes (
    strPassword : String, 
    rgbSalt : byte[]
)

パラメータ

strPassword

キー派生させる対象パスワード

rgbSalt

キー派生させるために使用するキー salt

解説解説
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
PasswordDeriveBytes クラス
PasswordDeriveBytes メンバ
System.Security.Cryptography 名前空間
その他の技術情報
暗号サービス

PasswordDeriveBytes コンストラクタ (String, Byte[], CspParameters)

キー派生させるために使用するパスワードキー salt、および暗号サービス プロバイダ (CSP : Cryptographic Service Provider) のパラメータ指定して、PasswordDeriveBytes クラス新しインスタンス初期化します。

名前空間: System.Security.Cryptography
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Sub New ( _
    strPassword As String, _
    rgbSalt As Byte(), _
    cspParams As CspParameters _
)
Dim strPassword As String
Dim rgbSalt As Byte()
Dim cspParams As CspParameters

Dim instance As New PasswordDeriveBytes(strPassword,
 rgbSalt, cspParams)
public PasswordDeriveBytes (
    string strPassword,
    byte[] rgbSalt,
    CspParameters cspParams
)
public:
PasswordDeriveBytes (
    String^ strPassword, 
    array<unsigned char>^ rgbSalt, 
    CspParameters^ cspParams
)
public PasswordDeriveBytes (
    String strPassword, 
    byte[] rgbSalt, 
    CspParameters cspParams
)
public function PasswordDeriveBytes (
    strPassword : String, 
    rgbSalt : byte[], 
    cspParams : CspParameters
)

パラメータ

strPassword

キー派生させる対象パスワード

rgbSalt

キー派生させるために使用するキー salt

cspParams

演算使用する CSP パラメータ

解説解説
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
PasswordDeriveBytes クラス
PasswordDeriveBytes メンバ
System.Security.Cryptography 名前空間
その他の技術情報
暗号サービス

PasswordDeriveBytes コンストラクタ (String, Byte[], String, Int32)

キー派生させるために使用するパスワードキー saltハッシュ名、および反復処理回数指定して、PasswordDeriveBytes クラス新しインスタンス初期化します。

名前空間: System.Security.Cryptography
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Sub New ( _
    strPassword As String, _
    rgbSalt As Byte(), _
    strHashName As String, _
    iterations As Integer _
)
Dim strPassword As String
Dim rgbSalt As Byte()
Dim strHashName As String
Dim iterations As Integer

Dim instance As New PasswordDeriveBytes(strPassword,
 rgbSalt, strHashName, iterations)
public PasswordDeriveBytes (
    string strPassword,
    byte[] rgbSalt,
    string strHashName,
    int iterations
)
public:
PasswordDeriveBytes (
    String^ strPassword, 
    array<unsigned char>^ rgbSalt, 
    String^ strHashName, 
    int iterations
)
public PasswordDeriveBytes (
    String strPassword, 
    byte[] rgbSalt, 
    String strHashName, 
    int iterations
)
public function PasswordDeriveBytes (
    strPassword : String, 
    rgbSalt : byte[], 
    strHashName : String, 
    iterations : int
)

パラメータ

strPassword

キー派生させる対象パスワード

rgbSalt

キー派生させるために使用するキー salt

strHashName

演算使用するハッシュ アルゴリズムの名前。

iterations

演算反復処理回数

解説解説
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
PasswordDeriveBytes クラス
PasswordDeriveBytes メンバ
System.Security.Cryptography 名前空間
その他の技術情報
暗号サービス

PasswordDeriveBytes コンストラクタ (Byte[], Byte[])

メモ : このコンストラクタは、.NET Framework version 2.0新しく追加されたものです。

キー派生させるために使用するパスワードおよびキー salt指定して、PasswordDeriveBytes クラス新しインスタンス初期化します。

名前空間: System.Security.Cryptography
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

public PasswordDeriveBytes (
    byte[] password,
    byte[] salt
)
public:
PasswordDeriveBytes (
    array<unsigned char>^ password, 
    array<unsigned char>^ salt
)
public PasswordDeriveBytes (
    byte[] password, 
    byte[] salt
)
public function PasswordDeriveBytes (
    password : byte[], 
    salt : byte[]
)

パラメータ

password

キー派生させる対象パスワード

salt

キー派生させるために使用するキー salt

解説解説
使用例使用例

PasswordDeriveBytes クラス使用してパスワードからキー作成するコード例次に示します

Imports System
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:")

        '********************************************************
        '* Security Note: Never hard-code a password within your
        '* source code.  Hard-coded passwords can be retrieved 
        '* from a compiled assembly.
        '********************************************************
        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 add it to the Key property.
            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 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
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;
        }
    }
}
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>^ password = Encoding::Unicode->GetBytes(Console::ReadLine());

    array<Byte>^ randomSalt = CreateRandomSalt(7);

    // Create a TripleDESCryptoServiceProvider object.
    TripleDESCryptoServiceProvider^ cryptoDESProvider = 
        gcnew 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^ passwordDeriveBytes = gcnew PasswordDeriveBytes
            (password->ToString(), randomSalt);

        // Create the key and add it to the Key property.
        cryptoDESProvider->Key = passwordDeriveBytes->CryptDeriveKey
            ("TripleDES", "SHA1", 192, cryptoDESProvider->IV);

        Console::WriteLine("Operation complete.");
    }
    catch (Exception^ ex)
    {
        Console::WriteLine(ex->Message);
    }
    finally
    {
        // Clear the buffers
        ClearBytes(password);
        ClearBytes(randomSalt);

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

    Console::ReadLine();
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
PasswordDeriveBytes クラス
PasswordDeriveBytes メンバ
System.Security.Cryptography 名前空間
その他の技術情報
暗号サービス

PasswordDeriveBytes コンストラクタ (Byte[], Byte[], CspParameters)

メモ : このコンストラクタは、.NET Framework version 2.0新しく追加されたものです。

キー派生させるために使用するパスワードキー salt、および暗号化サービス プロバイダ (CSP: Cryptographic Service Provider) を指定して、PasswordDeriveBytes クラス新しインスタンス初期化します。

名前空間: System.Security.Cryptography
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Sub New ( _
    password As Byte(), _
    salt As Byte(), _
    cspParams As CspParameters _
)
Dim password As Byte()
Dim salt As Byte()
Dim cspParams As CspParameters

Dim instance As New PasswordDeriveBytes(password,
 salt, cspParams)
public PasswordDeriveBytes (
    byte[] password,
    byte[] salt,
    CspParameters cspParams
)
public:
PasswordDeriveBytes (
    array<unsigned char>^ password, 
    array<unsigned char>^ salt, 
    CspParameters^ cspParams
)
public PasswordDeriveBytes (
    byte[] password, 
    byte[] salt, 
    CspParameters cspParams
)
public function PasswordDeriveBytes (
    password : byte[], 
    salt : byte[], 
    cspParams : CspParameters
)

パラメータ

password

キー派生させる対象パスワード

salt

キー派生させるために使用するキー salt

cspParams

この操作を行うための暗号化サービス プロバイダ (CSP) パラメータ

解説解説
使用例使用例

PasswordDeriveBytes クラス使用してパスワードからキー作成するコード例次に示します

Imports System
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:")

        '********************************************************
        '* Security Note: Never hard-code a password within your
        '* source code.  Hard-coded passwords can be retrieved 
        '* from a compiled assembly.
        '********************************************************
        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 add it to the Key property.
            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 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
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;
        }
    }
}
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>^ password = Encoding::Unicode->GetBytes(Console::ReadLine());

    array<Byte>^ randomSalt = CreateRandomSalt(7);

    // Create a TripleDESCryptoServiceProvider object.
    TripleDESCryptoServiceProvider^ cryptoDESProvider = 
        gcnew 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^ passwordDeriveBytes = gcnew PasswordDeriveBytes
            (password->ToString(), randomSalt);

        // Create the key and add it to the Key property.
        cryptoDESProvider->Key = passwordDeriveBytes->CryptDeriveKey
            ("TripleDES", "SHA1", 192, cryptoDESProvider->IV);

        Console::WriteLine("Operation complete.");
    }
    catch (Exception^ ex)
    {
        Console::WriteLine(ex->Message);
    }
    finally
    {
        // Clear the buffers
        ClearBytes(password);
        ClearBytes(randomSalt);

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

    Console::ReadLine();
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
PasswordDeriveBytes クラス
PasswordDeriveBytes メンバ
System.Security.Cryptography 名前空間
その他の技術情報
暗号サービス

PasswordDeriveBytes コンストラクタ (Byte[], Byte[], String, Int32)

メモ : このコンストラクタは、.NET Framework version 2.0新しく追加されたものです。

キー派生させるために使用するパスワードキー saltハッシュ名、および反復処理指定して、PasswordDeriveBytes クラス新しインスタンス初期化します。

名前空間: System.Security.Cryptography
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Sub New ( _
    password As Byte(), _
    salt As Byte(), _
    hashName As String, _
    iterations As Integer _
)
Dim password As Byte()
Dim salt As Byte()
Dim hashName As String
Dim iterations As Integer

Dim instance As New PasswordDeriveBytes(password,
 salt, hashName, iterations)
public PasswordDeriveBytes (
    byte[] password,
    byte[] salt,
    string hashName,
    int iterations
)
public:
PasswordDeriveBytes (
    array<unsigned char>^ password, 
    array<unsigned char>^ salt, 
    String^ hashName, 
    int iterations
)
public PasswordDeriveBytes (
    byte[] password, 
    byte[] salt, 
    String hashName, 
    int iterations
)
public function PasswordDeriveBytes (
    password : byte[], 
    salt : byte[], 
    hashName : String, 
    iterations : int
)

パラメータ

password

キー派生させる対象パスワード

salt

キー派生させるために使用するキー salt

hashName

キー派生させるために使用するハッシュ アルゴリズム

iterations

キー派生させるために使用する反復処理回数

解説解説
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
PasswordDeriveBytes クラス
PasswordDeriveBytes メンバ
System.Security.Cryptography 名前空間
その他の技術情報
暗号サービス

PasswordDeriveBytes コンストラクタ (Byte[], Byte[], String, Int32, CspParameters)

メモ : このコンストラクタは、.NET Framework version 2.0新しく追加されたものです。

キー派生させるために使用するパスワードキー saltハッシュ名、反復処理、および暗号化サービス プロバイダ (CSP: Cryptographic Service Provider) を指定して、PasswordDeriveBytes クラス新しインスタンス初期化します。

名前空間: System.Security.Cryptography
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Sub New ( _
    password As Byte(), _
    salt As Byte(), _
    hashName As String, _
    iterations As Integer, _
    cspParams As CspParameters _
)
Dim password As Byte()
Dim salt As Byte()
Dim hashName As String
Dim iterations As Integer
Dim cspParams As CspParameters

Dim instance As New PasswordDeriveBytes(password,
 salt, hashName, iterations, cspParams)
public PasswordDeriveBytes (
    byte[] password,
    byte[] salt,
    string hashName,
    int iterations,
    CspParameters cspParams
)
public:
PasswordDeriveBytes (
    array<unsigned char>^ password, 
    array<unsigned char>^ salt, 
    String^ hashName, 
    int iterations, 
    CspParameters^ cspParams
)
public PasswordDeriveBytes (
    byte[] password, 
    byte[] salt, 
    String hashName, 
    int iterations, 
    CspParameters cspParams
)
public function PasswordDeriveBytes (
    password : byte[], 
    salt : byte[], 
    hashName : String, 
    iterations : int, 
    cspParams : CspParameters
)

パラメータ

password

キー派生させる対象パスワード

salt

キー派生させるために使用するキー salt

hashName

キー派生させるために使用するハッシュ アルゴリズム

iterations

キー派生させるために使用する反復処理回数

cspParams

この操作を行うための暗号化サービス プロバイダ (CSP) パラメータ

解説解説
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
PasswordDeriveBytes クラス
PasswordDeriveBytes メンバ
System.Security.Cryptography 名前空間
その他の技術情報
暗号サービス

PasswordDeriveBytes コンストラクタ

PasswordDeriveBytes クラス新しインスタンス初期化します。
オーバーロードの一覧オーバーロードの一覧

名前 説明
PasswordDeriveBytes (Byte[], Byte[]) キー派生させるために使用するパスワードおよびキー salt指定してPasswordDeriveBytes クラス新しインスタンス初期化します。
PasswordDeriveBytes (String, Byte[]) キー派生させるために使用するパスワードおよびキー salt指定してPasswordDeriveBytes クラス新しインスタンス初期化します。
PasswordDeriveBytes (Byte[], Byte[], CspParameters) キー派生させるために使用するパスワードキー salt、および暗号化サービス プロバイダ (CSP: Cryptographic Service Provider) を指定してPasswordDeriveBytes クラス新しインスタンス初期化します。
PasswordDeriveBytes (String, Byte[], CspParameters) キー派生させるために使用するパスワードキー salt、および暗号サービス プロバイダ (CSP : Cryptographic Service Provider) のパラメータ指定してPasswordDeriveBytes クラス新しインスタンス初期化します。
PasswordDeriveBytes (Byte[], Byte[], String, Int32) キー派生させるために使用するパスワードキー saltハッシュ名、および反復処理指定してPasswordDeriveBytes クラス新しインスタンス初期化します。
PasswordDeriveBytes (String, Byte[], String, Int32) キー派生させるために使用するパスワードキー saltハッシュ名、および反復処理回数指定してPasswordDeriveBytes クラス新しインスタンス初期化します。
PasswordDeriveBytes (Byte[], Byte[], String, Int32, CspParameters) キー派生させるために使用するパスワードキー saltハッシュ名、反復処理、および暗号化サービス プロバイダ (CSP: Cryptographic Service Provider) を指定してPasswordDeriveBytes クラス新しインスタンス初期化します。
PasswordDeriveBytes (String, Byte[], String, Int32, CspParameters) キー派生するために使用するパスワードキー saltハッシュ名、反復処理回数、および暗号サービス プロバイダ (CSP) のパラメータ指定してPasswordDeriveBytes クラス新しインスタンス初期化します。
参照参照

関連項目

PasswordDeriveBytes クラス
PasswordDeriveBytes メンバ
System.Security.Cryptography 名前空間

その他の技術情報

暗号サービス

PasswordDeriveBytes コンストラクタ (String, Byte[], String, Int32, CspParameters)

キー派生するために使用するパスワードキー saltハッシュ名、反復処理回数、および暗号サービス プロバイダ (CSP) のパラメータ指定して、PasswordDeriveBytes クラス新しインスタンス初期化します。

名前空間: System.Security.Cryptography
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Sub New ( _
    strPassword As String, _
    rgbSalt As Byte(), _
    strHashName As String, _
    iterations As Integer, _
    cspParams As CspParameters _
)
Dim strPassword As String
Dim rgbSalt As Byte()
Dim strHashName As String
Dim iterations As Integer
Dim cspParams As CspParameters

Dim instance As New PasswordDeriveBytes(strPassword,
 rgbSalt, strHashName, iterations, cspParams)
public PasswordDeriveBytes (
    string strPassword,
    byte[] rgbSalt,
    string strHashName,
    int iterations,
    CspParameters cspParams
)
public:
PasswordDeriveBytes (
    String^ strPassword, 
    array<unsigned char>^ rgbSalt, 
    String^ strHashName, 
    int iterations, 
    CspParameters^ cspParams
)
public PasswordDeriveBytes (
    String strPassword, 
    byte[] rgbSalt, 
    String strHashName, 
    int iterations, 
    CspParameters cspParams
)
public function PasswordDeriveBytes (
    strPassword : String, 
    rgbSalt : byte[], 
    strHashName : String, 
    iterations : int, 
    cspParams : CspParameters
)

パラメータ

strPassword

キー派生させる対象パスワード

rgbSalt

キー派生させるために使用するキー salt

strHashName

演算使用するハッシュ アルゴリズムの名前。

iterations

演算反復処理回数

cspParams

演算使用する CSP パラメータ

解説解説
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
PasswordDeriveBytes クラス
PasswordDeriveBytes メンバ
System.Security.Cryptography 名前空間
その他の技術情報
暗号サービス

PasswordDeriveBytes プロパティ


パブリック プロパティパブリック プロパティ

参照参照

関連項目

PasswordDeriveBytes クラス
System.Security.Cryptography 名前空間

その他の技術情報

暗号サービス

PasswordDeriveBytes メソッド


パブリック メソッドパブリック メソッド

プロテクト メソッドプロテクト メソッド
参照参照

関連項目

PasswordDeriveBytes クラス
System.Security.Cryptography 名前空間

その他の技術情報

暗号サービス

PasswordDeriveBytes メンバ

PBKDF1 アルゴリズム拡張機能使用してパスワードからキー派生させます

PasswordDeriveBytes データ型公開されるメンバを以下の表に示します


パブリック コンストラクタパブリック コンストラクタ
  名前 説明
パブリック メソッド PasswordDeriveBytes オーバーロードされます。 PasswordDeriveBytes クラス新しインスタンス初期化します。
パブリック プロパティパブリック プロパティ
パブリック メソッドパブリック メソッド
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

PasswordDeriveBytes クラス
System.Security.Cryptography 名前空間

その他の技術情報

暗号サービス



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「PasswordDeriveBytes」の関連用語

PasswordDeriveBytesのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



PasswordDeriveBytesのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS