Hi Sir or Madam,
I am deeply urgently needed help on PBKDF2 matter. I had build a local external function for this. (See Below). But, i get error which i spent days still couldnt know how to solve it. Can anyone help me? I am totally new on external function. Luckily there Topwiz free code for me to use and refer. I just followed some of the sample. But still have problem . Please see below for my codes. Your help is highly appreciated.
(A)
Function ulong BCryptDeriveKeyPBKDF2 ( &
longptr hPrf, &
blob pbPassword, &
ulong cbPassword, &
blob pbSalt, &
ulong cbSalt, &
ulong cIterations, &
Ref blob pbDerivedKey, &
ulong cbDerivedKey, &
ulong dwFlags &
) Library "bcrypt.dll"
***********************************************************
(B)
//PBKDF2 - start
ULONG lul_salt
BLOB lblb_key
BLOB lblb_password
BLOB lblb_salt
//to generate random number - salt
of_gensalt(lul_salt)
messagebox('Ul_salt',string(lul_salt))
//*************
// not using the Ansi/Unicode option
lblb_password = Blob(ls_password, EncodingAnsi!)
lblb_salt = Blob(string(lul_salt))
// generate
lblb_key =in_bc.of_getderivedkey(lblb_password, lblb_salt)
If IsNull(lblb_key) Then
MessageBox(in_bc.LastFunction + " Failed", &
in_bc.LastErrText, StopSign!)
Return 1
End If
//*************
//PBKDF2 - end
***********************************************************
(C) - of_gensalt
aul_Random = in_bc.of_RandomNbr()
If IsNull(aul_Random) Then
MessageBox(in_bc.LastFunction + " Failed", &
in_bc.LastErrText, StopSign!)
Return
End If
***********************************************************
(D) - of_getderivedkey
public function blob of_getderivedkey (ref blob alblb_password, ref blob alblb_salt);
BLOB lblb_resultkey
BLOB lblb_NullReturn
ULONG lul_password_len
ULONG lul_salt_len
SetNull(lblb_NullReturn)
// open an algorithm handle
If Not NT_SUCCESS(BCryptOpenAlgorithmProvider(il_hRanAlg, &
BCRYPT_SHA512_ALGORITHM, 0, 0)) Then
of_ErrorCleanup("BCryptOpenAlgorithmProvider")
Return lblb_NullReturn
End If
lul_password_len= len(alblb_password)
lul_salt_len = len(alblb_salt)
// Allocate Plain Text Buffer
//lblb_resultkey = Blob(Space(256), iEncoding)
messagebox('il_hAlg',string(il_hAlg))
If Not NT_SUCCESS(BCryptDeriveKeyPBKDF2(il_hAlg, &
alblb_password, lul_password_len, &
alblb_salt, lul_salt_len, &
1000, lblb_resultkey, 0 ,0))Then
of_ErrorCleanup("BCryptDeriveKeyPBKDF2")
Return lblb_NullReturn
End If
// trim off any extra space
//lblb_resultkey = BlobMid(lblb_resultkey, 1, 256)
of_FinalCleanup()
//
Return lblb_resultkey
1. May i know why you know cbDerivedKey is 256, by looking at my code? I am still fresh. Can you enlighten me.
2. As per my code (Section B) , i have this line(see below x). I need to turn the lul_salt to string first, before making it to Blob databtype. If i didnt turn it to string first(see below y) ,i get assertion failure error. So, my question is, is that the proper/correct way to turn it to string before turn it to blob?
y ----> lblb_salt = Blob(lul_salt)
x ---> lblb_salt = Blob(string(lul_salt))
2. I suppose that is correct, but as I have never attempted to use these API functions, I cannot say with 100% certainty. I just refer to the documentation.
As for the new problem, I suggest you examine (again) Roland's BCrypt free code sample at TopWizProgramming web site, as I see his example program calls the BCryptOpenAlgorithmProvider API function. I've never coded/used any BCrypt API functions. It's very late here and I'm heading for bed. Good Luck!