Rfc2898DeriveBytes クラスとは? わかりやすく解説

Rfc2898DeriveBytes クラス

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

HMACSHA1 に基づく擬似乱数ジェネレータ使用してパスワード ベースキー派生機能 (PBKDF2) を実装ます。

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

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

Rfc2898DeriveBytes引数として、パスワードsalt反復回数受け取り、GetBytes メソッド呼び出してキー生成します

RFC 2898 には、パスワードsalt からキーおよび初期化ベクタ (IV) を作成するためのメソッド規定されています。実質的に無制限長さキー生成できる擬似乱数関数と、パスワード ベースキー派生関数である PBKDF2使用してキー派生させることができますRfc2898DeriveBytes クラスは、基本キーおよびその他のパラメータから派生キー生成するときに使用できますパスワード ベースキー派生関数では、基本キーパスワードに、その他のパラメータsalt 値および反復回数なります

PBKDF2詳細については、RFCWeb サイト (http://www.rfc-editor.org) で、「RFC 2898, PKCS #5: Password-Based Cryptography Specification Version 2.0」を参照してください。特に、セクション 5.2 の「PBKDF2」を参照してください

セキュリティに関するメモセキュリティに関するメモ

パスワードソース コード内にハード コーディングすることは絶対に避けてくださいハード コーディングされたパスワードは、MSIL 逆アセンブラ (Ildasm.exe) や 16 進エディタ使用したり、アセンブリNotepad.exe などのテキスト エディタ開いたりすることによって簡単にアセンブリから取得されしまいます

使用例使用例

Rfc2898DeriveBytes クラス使用してTripleDES クラスのまったく同じ 2 つキー作成するコード例次に示します次に、このキー使用して一部データ暗号化および復号化ます。

using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;

public class rfc2898test
{
    // Generate a key k1 with password pwd1 and salt salt1.
    // Generate a key k2 with password pwd1 and salt salt1.
    // Encrypt data1 with key k1 using symmetric encryption, creating
 edata1.
    // Decrypt edata1 with key k2 using symmetric decryption, creating
 data2.
    // data2 should equal data1.

    private const string
 usageText = "Usage: RFC2898 <password>\nYou must specify the password
 for encryption.\n";
    public static void Main(string[]
 passwordargs)
    {
        //If no file name is specified, write usage text.
        if (passwordargs.Length == 0)
        {
            Console.WriteLine(usageText);
        }
        else
        {
            string pwd1 = passwordargs[0];
            
            byte[] salt1 = new byte[] { 0x00, 0x01, 0x02, 0x03,
 0x04, 0x05, 0x06, 0xF1, 0xF0, 0xEE, 0x21, 0x22, 0x45};
            //data1 can be a string or contents of a file.
            string data1 = "Some test data";
            //The default iteration count is 1000 so the two methods
 use the same iteration count.
            int myIterations = 1000;
            try
            {
                Rfc2898DeriveBytes k1 = new Rfc2898DeriveBytes(pwd1,
 salt1,myIterations);
                Rfc2898DeriveBytes k2 = new Rfc2898DeriveBytes(pwd1,
 salt1);
                // Encrypt the data.
                TripleDES encAlg = TripleDES.Create();
                encAlg.Key = k1.GetBytes(16);
                MemoryStream encryptionStream = new MemoryStream();
                CryptoStream encrypt = new CryptoStream(encryptionStream,
 encAlg.CreateEncryptor(),CryptoStreamMode.Write);
                byte[] utfD1 = new System.Text.UTF8Encoding(false).GetBytes(data1);

                encrypt.Write(utfD1, 0, utfD1.Length);
                encrypt.FlushFinalBlock();
                encrypt.Close();
                byte[] edata1 = encryptionStream.ToArray();
                k1.Reset();

                // Try to decrypt, thus showing it can be round-tripped.
                TripleDES decAlg = TripleDES.Create();
                decAlg.Key = k2.GetBytes(16);
                decAlg.IV = encAlg.IV;
                MemoryStream decryptionStreamBacking = new MemoryStream();
                CryptoStream decrypt = new CryptoStream(decryptionStreamBacking,decAlg.CreateDecryptor(),
 CryptoStreamMode.Write);
                decrypt.Write(edata1, 0, edata1.Length);
                decrypt.Flush();
                decrypt.Close();
                k2.Reset();
                string data2 = new UTF8Encoding(false).GetString(decryptionStreamBacking.ToArray());

                if (!data1.Equals(data2))
                {
                    Console.WriteLine("Error: The two values are not equal.");
                }
                else
                {
                    Console.WriteLine("The two values are equal.");
                    Console.WriteLine("k1 iterations: {0}",k1.IterationCount);
                    Console.WriteLine("k2 iterations: {0}",k2.IterationCount);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: ",e);
            }

        }
    }
}
using namespace System;
using namespace System::IO;
using namespace System::Text;
using namespace System::Security::Cryptography;

// Generate a key k1 with password pwd1 and salt salt1.
// Generate a key k2 with password pwd1 and salt salt1.
// Encrypt data1 with key k1 using symmetric encryption, creating edata1.
// Decrypt edata1 with key k2 using symmetric decryption, creating data2.
// data2 should equal data1.

int main()
{
   array<String^>^passwordargs = Environment::GetCommandLineArgs();
   String^ usageText = "Usage: RFC2898 <password>\nYou must specify the
 password for encryption.\n";

   //If no file name is specified, write usage text.
   if ( passwordargs->Length == 1 )
   {
      Console::WriteLine( usageText );
   }
   else
   {
      String^ pwd1 = passwordargs[ 1 ];
      
      array<Byte>^salt1 = gcnew array<Byte>{
         0x00,0x01,0x02,0x03,0x04,0x05,0x06,0xF1,0xF0,0xEE,0x21,0x22,0x45
      };

      //data1 can be a string or contents of a file.
      String^ data1 = "Some test data";

      //The default iteration count is 1000 so the two methods use the
 same iteration count.
      int myIterations = 1000;

      try
      {
         Rfc2898DeriveBytes ^ k1 = gcnew Rfc2898DeriveBytes( pwd1,salt1,myIterations
 );
         Rfc2898DeriveBytes ^ k2 = gcnew Rfc2898DeriveBytes( pwd1,salt1 );

         // Encrypt the data.
         TripleDES^ encAlg = TripleDES::Create();
         encAlg->Key = k1->GetBytes( 16 );
         MemoryStream^ encryptionStream = gcnew MemoryStream;
         CryptoStream^ encrypt = gcnew CryptoStream( encryptionStream,encAlg->CreateEncryptor(),CryptoStreamMode::Write
 );
         array<Byte>^utfD1 = (gcnew System::Text::UTF8Encoding( false
 ))->GetBytes( data1 );

         encrypt->Write( utfD1, 0, utfD1->Length );
         encrypt->FlushFinalBlock();
         encrypt->Close();
         array<Byte>^edata1 = encryptionStream->ToArray();
         k1->Reset();

         // Try to decrypt, thus showing it can be round-tripped.
         TripleDES^ decAlg = TripleDES::Create();
         decAlg->Key = k2->GetBytes( 16 );
         decAlg->IV = encAlg->IV;
         MemoryStream^ decryptionStreamBacking = gcnew MemoryStream;
         CryptoStream^ decrypt = gcnew CryptoStream( decryptionStreamBacking,decAlg->CreateDecryptor(),CryptoStreamMode::Write
 );

         decrypt->Write( edata1, 0, edata1->Length );
         decrypt->Flush();
         decrypt->Close();
         k2->Reset();

         String^ data2 = (gcnew UTF8Encoding( false ))->GetString(
 decryptionStreamBacking->ToArray() );
         if (  !data1->Equals( data2 ) )
         {
            Console::WriteLine( "Error: The two values are not equal."
 );
         }
         else
         {
            Console::WriteLine( "The two values are equal." );
            Console::WriteLine( "k1 iterations: {0}", k1->IterationCount
 );
            Console::WriteLine( "k2 iterations: {0}", k2->IterationCount
 );
         }
      }

      catch ( Exception^ e ) 
      {
         Console::WriteLine( "Error: ", e );
      }
   }
}
import System.*;
import System.IO.*;
import System.Text.*;
import System.Security.Cryptography.*;

public class rfc2898test
{
    // Generate a key k1 with password pwd1 and salt salt1.
    // Generate a key k2 with password pwd1 and salt salt1.
    // Encrypt data1 with key k1 using symmetric encryption, creating
 eData1.
    // Decrypt eData1 with key k2 using symmetric decryption, creating
 data2.
    // data2 should equal data1.
    private static String usageText = "Usage:
 RFC2898 <password>\n"
        + "You must specify the password for encryption.\n";

    public static void main(String[]
 passwordArgs)
    {
        //If no file name is specified, write usage text.
        if (passwordArgs.get_Length() == 0) {
            Console.WriteLine(usageText);
        }
        else {
            String pwd1 = (String)passwordArgs.get_Item(0);

            ubyte salt1[] = new ubyte[] { 0x0, 0x1, 0x2, 0x3,
 0x4, 0x5, 0x6,
                0xF1, 0xF0, 0xEE, 0x21, 0x22, 0x45 };
            //data1 can be a string or contents of a file.
            String data1 = "Some test data";
            //The default iteration count is 1000 so the two methods
 use the
            //same iteration count.
            int myIterations = 1000;
            try {
                Rfc2898DeriveBytes k1 = new Rfc2898DeriveBytes(pwd1,
 salt1,
                    myIterations);
                Rfc2898DeriveBytes k2 = new Rfc2898DeriveBytes(pwd1,
 salt1);
                // Encrypt the data.
                TripleDES encAlg = TripleDES.Create();
                encAlg.set_Key(k1.GetBytes(16));
                MemoryStream encryptionStream = new MemoryStream();
                CryptoStream encrypt = new CryptoStream(encryptionStream,
 
                    encAlg.CreateEncryptor(), CryptoStreamMode.Write);
                ubyte utfD1[] = (new System.Text.UTF8Encoding(false)).
                    GetBytes(data1);
                encrypt.Write(utfD1, 0, utfD1.get_Length());
                encrypt.FlushFinalBlock();
                encrypt.Close();
                ubyte eData1[] = encryptionStream.ToArray();
                k1.Reset();
                // Try to decrypt, thus showing it can be round-tripped.
                TripleDES decAlg = TripleDES.Create();
                decAlg.set_Key(k2.GetBytes(16));
                decAlg.set_IV(encAlg.get_IV());
                MemoryStream decryptionStreamBacking = new MemoryStream();
                CryptoStream decrypt = new CryptoStream(decryptionStreamBacking,
 
                    decAlg.CreateDecryptor(), CryptoStreamMode.Write);
                decrypt.Write(eData1, 0, eData1.get_Length());
                decrypt.Flush();
                decrypt.Close();
                k2.Reset();
                String data2 = (new UTF8Encoding(false)).
                    GetString(decryptionStreamBacking.ToArray());
                if (!(data1.Equals(data2))) {
                    Console.WriteLine("Error: The two values are not equal.");
                }
                else {
                    Console.WriteLine("The two values are equal.");
                    Console.WriteLine("k1 iterations: {0}", (System.Int32)k1.
                        get_IterationCount());
                    Console.WriteLine("k2 iterations: {0}", (System.Int32)k2.
                        get_IterationCount());
                }
            }           
            catch (System.Exception e) {
                Console.WriteLine("Error: ", e);
            }
        }
    } //main
} //rfc2898test
継承階層継承階層
System.Object
   System.Security.Cryptography.DeriveBytes
    System.Security.Cryptography.Rfc2898DeriveBytes
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からRfc2898DeriveBytes クラスを検索した結果を表示しています。
Weblioに収録されているすべての辞書からRfc2898DeriveBytes クラスを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からRfc2898DeriveBytes クラス を検索

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

辞書ショートカット

すべての辞書の索引

「Rfc2898DeriveBytes クラス」の関連用語

Rfc2898DeriveBytes クラスのお隣キーワード
検索ランキング

   

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



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

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

©2025 GRAS Group, Inc.RSS