CryptoSys API examples VB6 to VB.NET

PBE_Kdf2Hex

Derives a key of any length from a password using the PBKDF2 algorithm from PKCS #5 with the salt and output derived key encoded in hexadecimal.

VB6/VBA

Debug.Print "Testing PBE_Kdf2Hex ..."
Dim strDerivedKey As String
Dim nKeyLen As Long
Dim strPassword As String
Dim strSaltHex As String
Dim nCount As Long
Dim nRet As Long

strPassword = "password"  ' NB normal text, not hex

' Set 8-byte salt = 78 57 8E 5A 5D 63 CB 06
strSaltHex = "78578E5A5D63CB06"

' Iteration count is 2048
nCount = 2048

' Pre-dimension output string for derived key to
' required length of 24 bytes i.e. 48 hex chars
' (Don't forget to do this)
nKeyLen = 24
strDerivedKey = String(2 * nKeyLen, " ")

' Derive PBKDF2 key using function from CryptoSys API
nRet = PBE_Kdf2Hex(strDerivedKey, Len(strDerivedKey), nKeyLen, _
    strPassword, strSaltHex, nCount, 0)

' Check against test vector
Debug.Print "Derived key = " & strDerivedKey
Debug.Print "Correct key = BFDE6BE94DF7E11DD409BCE20A0255EC327CB936FFE93643"

Output

Testing PBE_Kdf2Hex ...
Derived key = BFDE6BE94DF7E11DD409BCE20A0255EC327CB936FFE93643
Correct key = BFDE6BE94DF7E11DD409BCE20A0255EC327CB936FFE93643

VB.NET

Console.WriteLine("Testing PBE_Kdf2Hex ...")
Dim strDerivedKey As String
Dim nKeyLen As Integer
Dim strPassword As String
Dim strSaltHex As String
Dim nCount As Integer
''Dim nRet As Integer

strPassword = "password"  ' NB normal text, not hex

' Set 8-byte salt = 78 57 8E 5A 5D 63 CB 06
strSaltHex = "78578E5A5D63CB06"

' Iteration count is 2048
nCount = 2048

nKeyLen = 24

' Derive PBKDF2 key using function from CryptoSys API
strDerivedKey = Pbe.Kdf2(nKeyLen, strPassword, strSaltHex, nCount)

' Check against test vector
Console.WriteLine("Derived key = " & strDerivedKey)
Console.WriteLine("Correct key = BFDE6BE94DF7E11DD409BCE20A0255EC327CB936FFE93643")

[Contents]

[HOME]   [NEXT: PC1_Bytes...]

Copyright © 2009-20 D.I. Management Services Pty Ltd. All rights reserved.