Rfc2898DeriveBytes クラス
アセンブリ: mscorlib (mscorlib.dll 内)


Rfc2898DeriveBytes は引数として、パスワード、salt、反復回数を受け取り、GetBytes メソッドを呼び出してキーを生成します。
RFC 2898 には、パスワードと salt からキーおよび初期化ベクタ (IV) を作成するためのメソッドが規定されています。実質的に無制限の長さのキーを生成できる擬似乱数関数と、パスワード ベースのキー派生関数である PBKDF2 を使用して、キーを派生させることができます。Rfc2898DeriveBytes クラスは、基本キーおよびその他のパラメータから派生キーを生成するときに使用できます。パスワード ベースのキー派生関数では、基本キーがパスワードに、その他のパラメータは salt 値および反復回数になります。
PBKDF2 の詳細については、RFC の Web サイト (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.Security.Cryptography.DeriveBytes
System.Security.Cryptography.Rfc2898DeriveBytes


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- Rfc2898DeriveBytes クラスのページへのリンク