Rfc2898DeriveBytes

Rfc2898DeriveBytes


Implements password-based key derivation functionality, PBKDF2, by using a pseudo-random number generator based on HMACSHA1.


Implements:

IObject 

Public:

Properties:

NameDescription
 IterationCount (get) Gets the number of iterations for the operation.  
 IterationCount (let) Sets the number of iterations for the operation.  
 Salt (get) Gets the key salt value for the operation.  
 Salt (let) Sets the key salt value for the operation.  

Methods:

NameDescription
 Equals This function determines if the value passed in is the same as the current object instance. Meaning, are the Value and this object the same object in memory.  
 GetBytes Returns the pseudo-random key for this object.  
 GetHashCode Returns a psuedo-unique number used to help identify this object in memory. The current method is to return the value obtained from ObjPtr. If a different method needs to be impelmented then change the method here in this function.

An override might be necessary if the hashcode should be derived from a value contained within the class.  

 Reset Resets the state of the operation.  
 ToString Returns a string representation of this object instance. The default method simply returns the application name and class name in which this class resides.

A Person class may return the persons name instead.  

Remarks

An Rfc2898DeriveBytes instance can be created using the NewRfc2898DeriveBytes constructor.

Rfc2898DeriveBytes takes a password, a salt, and an iteration count, and then generates keys through calls to the GetBytes method.

RFC 2898 includes methods for creating a key and initialization vector (IV) from a password and salt. You can use PBKDF2, a password-based key derivation function, to derive keys using a pseudo-random function that allows keys of virtually unlimited length to be generated. The Rfc2898DeriveBytes class can be used to produce a derived key from a base key and other parameters. In a password-based key derivation function, the base key is a password and the other parameters are a salt value and an iteration count.

Examples

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.

Public Sub Main()
    Const Pwd1 As String = "Simple Password"
    Dim Salt1(8) As Byte
    Dim RngCsp As New RNGCryptoServiceProvider
    
    RngCsp.GetBytes Salt1
    
    ' Data1 can be a string or contents of a file.
    Const Data1 As String = "Some test data"
    
    ' 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

    Exit Sub

Catch:
    Dim Ex As Exception
    
    Catch Ex, Err
    Debug.Print Ex.ToString
End Sub

' This example code produces the following output.
'
'    The two values are equal.
'    K1 iterations: 1000
'    K2 iterations: 1000

See Also

Project CorLib Overview

Class Rfc2898DeriveBytes Overview

Constructors