Rfc2898DeriveBytes: IterationCount (let)

IterationCount

Sets the number of iterations for the operation.



 Public Property Let IterationCount(
	  ByVal Value As Long )

Parameters

Value
[ByVal] Long. The number of iterations for the operation.

Remarks

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.

Read/Write.

Exceptions

ExceptionCondition
ArgumentOutOfRangeException The number of iterations is less than 1.

Examples

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.
    Const MyIterations As Long = 1000
    
    On Error GoTo Catch
    
    Dim K1 As Rfc2898DeriveBytes
    Dim K2 As Rfc2898DeriveBytes
    
    Set K1 = NewRfc2898DeriveBytes(Pwd1, Salt1, MyIterations)
    Set K2 = NewRfc2898DeriveBytes(Pwd1, Salt1)
    
    ' Encrypt the data.
    Dim EncAlg As TripleDES
    Dim EncryptionStream As New MemoryStream
    Dim Encrypt As CryptoStream
    Dim UtfD1() As Byte
    
    Set EncAlg = TripleDES.Create()
    EncAlg.Key = K1.GetBytes(16)
    Set Encrypt = NewCryptoStream(EncryptionStream, EncAlg.CreateEncryptor(), CryptoStreamMode.WriteMode)
    UtfD1 = NewUTF8Encoding(False).GetBytes(Data1)
    
    Encrypt.WriteBlock UtfD1, 0, CorArray.Length(UtfD1)
    Encrypt.FlushFinalBlock
    Encrypt.CloseStream
    
    Dim EData1() As Byte
    EData1 = EncryptionStream.ToArray()
    K1.Reset

    ' Try to decrypt, thus showing it can be round-tripped.
    Dim DecAlg As TripleDES
    Dim DecryptionStreamBacking As New MemoryStream
    Dim Decrypt As CryptoStream
        
    Set DecAlg = TripleDES.Create()
    DecAlg.Key = K2.GetBytes(16)
    DecAlg.IV = EncAlg.IV
    
    Set Decrypt = NewCryptoStream(DecryptionStreamBacking, DecAlg.CreateDecryptor(), CryptoStreamMode.WriteMode)
    Decrypt.WriteBlock EData1, 0, CorArray.Length(EData1)
    Decrypt.Flush
    Decrypt.CloseStream
    K2.Reset
    
    Dim Data2 As String
    Data2 = NewUTF8Encoding(False).GetString(DecryptionStreamBacking.ToArray())

    If Data1 <> Data2 Then
        Debug.Print "Error: The two values are not equal."
    Else
        Debug.Print "The two values are equal."
        Debug.Print CorString.Format("K1 iterations: {0}", K1.IterationCount)
        Debug.Print CorString.Format("K2 iterations: {0}", K2.IterationCount)
    End If

See Also

Project CorLib Overview

Class Rfc2898DeriveBytes Overview