Derives a key of any length from a password using the PBKDF2 algorithm from PKCS #5.
VB6/VBA
Debug.Print "Testing PBE_Kdf2 ..." Dim abDerivedKey() As Byte Dim nKeyLen As Long Dim sPassword As String Dim abPwdBytes() As Byte Dim abSalt(7) As Byte Dim nCount As Long Dim nRet As Long ' Convert password String to an array of Bytes sPassword = "password" abPwdBytes = StrConv(sPassword, vbFromUnicode) ' Set 8-byte salt = 78 57 8E 5A 5D 63 CB 06 abSalt(0) = &H78 abSalt(1) = &H57 abSalt(2) = &H8E abSalt(3) = &H5A abSalt(4) = &H5D abSalt(5) = &H63 abSalt(6) = &HCB abSalt(7) = &H6 ' Iteration count is 2048 nCount = 2048 ' Pre-dimension output for derived key to required length of 24 bytes ' (Don't forget to do this) nKeyLen = 24 ReDim abDerivedKey(nKeyLen - 1) ' Derive PBKDF2 key using function from CryptoSys nRet = PBE_Kdf2(abDerivedKey(0), nKeyLen, _ abPwdBytes(0), Len(sPassword), abSalt(0), 8&, nCount, 0&) ' Convert bytes to hex and print Debug.Print "Derived key = " & cnvHexStrFromBytes(abDerivedKey) Debug.Print "Correct key = BFDE6BE94DF7E11DD409BCE20A0255EC327CB936FFE93643"
Output
Testing PBE_Kdf2 ... Derived key = BFDE6BE94DF7E11DD409BCE20A0255EC327CB936FFE93643 Correct key = BFDE6BE94DF7E11DD409BCE20A0255EC327CB936FFE93643
VB.NET
Console.WriteLine("Testing PBE_Kdf2 ...") Dim abDerivedKey() As Byte Dim nKeyLen As Integer Dim sPassword As String Dim abPwdBytes() As Byte Dim abSalt() As Byte Dim nCount As Integer ''Dim nRet As Integer ' Convert password String to an array of Bytes sPassword = "password" abPwdBytes = System.Text.Encoding.Default.GetBytes(sPassword) ' Set 8-byte salt = 78 57 8E 5A 5D 63 CB 06 abSalt = New Byte() {&H78, &H57, &H8E, &H5A, &H5D, &H63, &HCB, &H6} ' Iteration count is 2048 nCount = 2048 ' Pre-dimension output for derived key to required length of 24 bytes nKeyLen = 24 ''ReDim abDerivedKey(nKeyLen - 1) ' Derive PBKDF2 key using function from CryptoSys abDerivedKey = Pbe.Kdf2(nKeyLen, abPwdBytes, abSalt, nCount) ' Convert bytes to hex and print Console.WriteLine("Derived key = " & Cnv.ToHex(abDerivedKey)) Console.WriteLine("Correct key = BFDE6BE94DF7E11DD409BCE20A0255EC327CB936FFE93643")
[Contents]