Aracılığıyla paylaş


Rfc2898DeriveBytes.IterationCount Özellik

Tanım

İşlem için yineleme sayısını alır veya ayarlar.

public:
 property int IterationCount { int get(); void set(int value); };
public int IterationCount { get; set; }
member this.IterationCount : int with get, set
Public Property IterationCount As Integer

Özellik Değeri

İşlemin yineleme sayısı.

Özel durumlar

Yineleme sayısı 1'den azdır.

Örnekler

Aşağıdaki örnekte, anahtarın oluşturulmasında IterationCount kullanılan yineleme sayısını görüntülemek için özelliğinin nasıl kullanılacağı gösterilmektedir. Bu kod örneği, sınıfı için Rfc2898DeriveBytes sağlanan daha büyük bir örneğin parçasıdır.

//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.
   Aes^ encAlg = Aes::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.
   Aes^ decAlg = Aes::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 );
   }
            //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.
                Aes encAlg = Aes.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.
                Aes decAlg = Aes.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);
                }
'The default iteration count is 1000 so the two methods use the same iteration count.
Dim myIterations As Integer = 1000
Try
    Dim k1 As New Rfc2898DeriveBytes(pwd1, salt1, myIterations)
    Dim k2 As New Rfc2898DeriveBytes(pwd1, salt1)
    ' Encrypt the data.
    Dim encAlg As Aes = Aes.Create()
    encAlg.Key = k1.GetBytes(16)
    Dim encryptionStream As New MemoryStream()
    Dim encrypt As New CryptoStream(encryptionStream, encAlg.CreateEncryptor(), CryptoStreamMode.Write)
    Dim utfD1 As Byte() = New System.Text.UTF8Encoding(False).GetBytes(data1)
    encrypt.Write(utfD1, 0, utfD1.Length)
    encrypt.FlushFinalBlock()
    encrypt.Close()
    Dim edata1 As Byte() = encryptionStream.ToArray()
    k1.Reset()

    ' Try to decrypt, thus showing it can be round-tripped.
    Dim decAlg As Aes = Aes.Create()
    decAlg.Key = k2.GetBytes(16)
    decAlg.IV = encAlg.IV
    Dim decryptionStreamBacking As New MemoryStream()
    Dim decrypt As New CryptoStream(decryptionStreamBacking, decAlg.CreateDecryptor(), CryptoStreamMode.Write)
    decrypt.Write(edata1, 0, edata1.Length)
    decrypt.Flush()
    decrypt.Close()
    k2.Reset()
    Dim data2 As String = New UTF8Encoding(False).GetString(decryptionStreamBacking.ToArray())

    If Not data1.Equals(data2) Then
        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)
    End If

Açıklamalar

Yineleme sayısı, bir işlemin kaç kez gerçekleştirildiğini gösterir. Bu yöntem için sayı sıfırdan büyük olmalıdır. Önerilen en az yineleme sayısı 1000'dir.

Şunlara uygulanır

Ayrıca bkz.