Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Rfc2898DeriveBytes.IterationCount Property

Gets or sets the number of iterations for the operation.

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

public int IterationCount { get; set; }

Property Value

Type: System.Int32
The number of iterations for the operation.


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;
                Rfc2898DeriveBytes k1 = new Rfc2898DeriveBytes(pwd1, salt1, 
                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(

                encrypt.Write(utfD1, 0, utfD1.Length);
                byte[] edata1 = encryptionStream.ToArray();

                // 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);
                string data2 = new UTF8Encoding(false).GetString(

                if (!data1.Equals(data2))
                    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);

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
© 2015 Microsoft