Results 1 to 6 of 6

Thread: [RESOLVED] PasswordDeriveBytes warning

Hybrid View

  1. #1
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 05
    Location
    Sexbierum (Netherlands)
    Posts
    2,174

    Resolved [RESOLVED] PasswordDeriveBytes warning

    Hi,

    I'm trying to decrypt a file with the following code:

    vb.net Code:
    1. Function DecryptAES(ByVal CipherText As String, ByVal password As String, ByVal salt As String) As String
    2.         Dim HashAlgorithm As String = h_alg
    3.         Dim PasswordIterations As Integer = 2
    4.         Dim InitialVector As String = iv
    5.         Dim KeySize As Integer = 256
    6.  
    7.         If (String.IsNullOrEmpty(CipherText)) Then
    8.             Return ""
    9.         End If
    10.  
    11.         Dim InitialVectorBytes As Byte() = Encoding.ASCII.GetBytes(InitialVector)
    12.         Dim SaltValueBytes As Byte() = Encoding.ASCII.GetBytes(sa)
    13.         Dim CipherTextBytes As Byte() = Convert.FromBase64String(CipherText)
    14.         Dim DerivedPassword As PasswordDeriveBytes = New PasswordDeriveBytes(pn, SaltValueBytes, HashAlgorithm, PasswordIterations)
    15.  
    16.         '  here is where the warning occurs
    17.         Dim KeyBytes As Byte() = DerivedPassword.GetBytes(CInt(KeySize / 8))
    18.        
    19.         '  etc....
    20.     End Function

    Code:
    Warning 1 'Public Overrides Function GetBytes(cb As Integer) As Byte()' is obsolete:
     'Rfc2898DeriveBytes replaces PasswordDeriveBytes for deriving key material from a password
    and is preferred in new applications.'.
    Anyone knows how to do this properly?


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,757

    Re: PasswordDeriveBytes warning

    Did you read the documentation for the Rfc2898DeriveBytes class?

  3. #3
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 05
    Location
    Sexbierum (Netherlands)
    Posts
    2,174

    Re: PasswordDeriveBytes warning

    Yeah, but it was all chinese to me at the moment. I do saerch before asking by the way.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,757

    Re: PasswordDeriveBytes warning

    I've never used either but from a couple of minutes looking at the doco it looks to be almost exactly the same as what you already have. The class constructor is overloaded but basically takes a password, salt and iteration count and then you call GetBytes.

  5. #5
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 05
    Location
    Sexbierum (Netherlands)
    Posts
    2,174

    Re: PasswordDeriveBytes warning

    Already tried something alike, but I think I have to take a little time away from it and try later. Been trying to fix this for a long time.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,757

    Re: PasswordDeriveBytes warning

    It seems relatively straightforward to me. When a user registers you create a key generator with the password, the length of the salt you want and the number of iterations to perform, then save the generated salt and key to the database:
    vb.net Code:
    1. Private Sub registerButton_Click(sender As System.Object, e As System.EventArgs) Handles registerButton.Click
    2.     Dim salt As Byte()
    3.     Dim key As Byte()
    4.  
    5.     Register(passwordTextBox.Text, salt, key)
    6.  
    7.     'Save salt and key to database.
    8. End Sub
    9.  
    10. Private Sub Register(password As String, ByRef salt As Byte(), ByRef key As Byte())
    11.     'Create a key generator for the password with a 16-byte salt value that uses 10 iterations.
    12.     Using generator As New Rfc2898DeriveBytes(password, 16, 10)
    13.         'Get the generated salt.
    14.         salt = generator.Salt
    15.  
    16.         'Get a 32-byte key.
    17.         key = generator.GetBytes(32)
    18.     End Using
    19. End Sub
    When the user logs on you get the salt and the key from that database, generate a key from the provided password and salt and see if it matches the stored value:
    vb.net Code:
    1. Private Sub logonButton_Click(sender As System.Object, e As System.EventArgs) Handles logonButton.Click
    2.     'Get salt and key from database.
    3.     Dim salt As Byte()
    4.     Dim key As Byte()
    5.  
    6.     If Logon(passwordTextBox.Text, salt, key) Then
    7.         'The logon was successful.
    8.     End If
    9. End Sub
    10.  
    11. Private Function Logon(password As String, salt As Byte(), expectedKey As Byte()) As Boolean
    12.     'Create a key generator for the password with the specified salt value that uses 10 iterations.
    13.     Using generator As New Rfc2898DeriveBytes(password, salt, 10)
    14.         'Get a 32-byte key.
    15.         Dim actualKey = generator.GetBytes(32)
    16.  
    17.         'Compare the actualKey to the expectedKey.
    18.         For i = 0 To actualKey.GetUpperBound(0)
    19.             If actualKey(i) <> expectedKey(i) Then
    20.                 'No match
    21.                 Return False
    22.             End If
    23.         Next
    24.  
    25.         'Match found
    26.         Return True
    27.     End Using
    28. End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •