IterationCount Property
Collapse the table of content
Expand the table of content

Rfc2898DeriveBytes.IterationCount Property


Gets or sets the number of iterations for the operation.

Namespace:   System.Security.Cryptography
Assembly:  mscorlib (in mscorlib.dll)

Public Property IterationCount As Integer

Property Value

Type: System.Int32

The number of iterations for the operation.

Exception Condition

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.
Dim myIterations As Integer = 1000
    Dim k1 As New Rfc2898DeriveBytes(pwd1, salt1, myIterations)
    Dim k2 As New Rfc2898DeriveBytes(pwd1, salt1)
    ' Encrypt the data.
    Dim encAlg As TripleDES = TripleDES.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)
    Dim edata1 As Byte() = encryptionStream.ToArray()

    ' Try to decrypt, thus showing it can be round-tripped.
    Dim decAlg As TripleDES = TripleDES.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)
    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.")
        Console.WriteLine("The two values are equal.")
        Console.WriteLine("k1 iterations: {0}", k1.IterationCount)
        Console.WriteLine("k2 iterations: {0}", k2.IterationCount)
    End If

Universal Windows Platform
Available since 10
.NET Framework
Available since 2.0
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top
© 2016 Microsoft