Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
IterationCount Property
Collapse the table of content
Expand the table of content

Rfc2898DeriveBytes.IterationCount Property

Note: This property is new in the .NET Framework version 2.0.

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 */
public int get_IterationCount ()

/** @property */
public void set_IterationCount (int value)

public function get IterationCount () : int

public function set IterationCount (value : int)

Property Value

The number of iterations for the operation.

Exception typeCondition

ArgumentException

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 code example uses the Rfc2898DeriveBytes class to create two identical keys for the TripleDES class. It then encrypts and decrypts some data using the keys.

//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.
    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(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.
    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);
    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.
int myIterations = 1000;
try {
    Rfc2898DeriveBytes k1 = new Rfc2898DeriveBytes(pwd1, salt1,
        myIterations);
    Rfc2898DeriveBytes k2 = new Rfc2898DeriveBytes(pwd1, salt1);
    // Encrypt the data.
    TripleDES encAlg = TripleDES.Create();
    encAlg.set_Key(k1.GetBytes(16));
    MemoryStream encryptionStream = new MemoryStream();
    CryptoStream encrypt = new CryptoStream(encryptionStream, 
        encAlg.CreateEncryptor(), CryptoStreamMode.Write);
    ubyte utfD1[] = (new System.Text.UTF8Encoding(false)).
        GetBytes(data1);
    encrypt.Write(utfD1, 0, utfD1.get_Length());
    encrypt.FlushFinalBlock();
    encrypt.Close();
    ubyte eData1[] = encryptionStream.ToArray();
    k1.Reset();
    // Try to decrypt, thus showing it can be round-tripped.
    TripleDES decAlg = TripleDES.Create();
    decAlg.set_Key(k2.GetBytes(16));
    decAlg.set_IV(encAlg.get_IV());
    MemoryStream decryptionStreamBacking = new MemoryStream();
    CryptoStream decrypt = new CryptoStream(decryptionStreamBacking, 
        decAlg.CreateDecryptor(), CryptoStreamMode.Write);
    decrypt.Write(eData1, 0, eData1.get_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}", (System.Int32)k1.
            get_IterationCount());
        Console.WriteLine("k2 iterations: {0}", (System.Int32)k2.
            get_IterationCount());
    }

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2015 Microsoft