Rfc2898DeriveBytes::IterationCount Property
Gets or sets the number of iterations for the operation.
Assembly: mscorlib (in mscorlib.dll)
| Exception | Condition |
|---|---|
| ArgumentOutOfRangeException | The number of iterations is less than 1. |
Iteration count is the number of times an operation is performed. For this method, the count should be greater than zero. The minimum recommended number of iterations is 1000.
The following example shows how to use the IterationCount property to display the number of iterations used in the generation of the key. This code example is part of a larger example provided for the Rfc2898DeriveBytes class.
//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 ); }
Available since 2.0
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0