1. Sim Joo Pee
  2. PowerBuilder
  3. Tuesday, 4 October 2022 00:28 AM UTC

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.


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"


//PBKDF2 - start
ULONG     lul_salt
BLOB        lblb_key
BLOB        lblb_password
BLOB        lblb_salt

//to generate random number - 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!)
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


// open an algorithm handle
If Not NT_SUCCESS(BCryptOpenAlgorithmProvider(il_hRanAlg, &
                        BCRYPT_SHA512_ALGORITHM, 0, 0)) Then
    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)
If Not NT_SUCCESS(BCryptDeriveKeyPBKDF2(il_hAlg, &
                        alblb_password, lul_password_len, &
                        alblb_salt, lul_salt_len, &
                        1000, lblb_resultkey, 0 ,0))Then
    Return lblb_NullReturn
End If

// trim off any extra space
//lblb_resultkey = BlobMid(lblb_resultkey, 1, 256)
Return lblb_resultkey



Sim Joo Pee Accepted Answer Pending Moderation
  1. Tuesday, 4 October 2022 02:28 AM UTC
  2. PowerBuilder
  3. # 1

Hi John, thanks. It work now after i change to longlong and 256. But, i received another error, telling me, "An invalid handle was specified".

Not the below one?

// open an algorithm handle
If Not NT_SUCCESS(BCryptOpenAlgorithmProvider(il_hRanAlg, &
                        BCRYPT_SHA512_ALGORITHM, 0, 0)) Then
    Return lblb_NullReturn
End If

There are no comments made yet.
John Fauss Accepted Answer Pending Moderation
  1. Tuesday, 4 October 2022 02:04 AM UTC
  2. PowerBuilder
  3. # 2

Are you getting runtime error(s)? If so, which ones? At what line of what function?

Looking at the WinAPI documentation for the BCryptDeriveKeyPBKDF2 function, the sixth argument (called cIterations in the documentation) is described as a ULONGLONG, which is a 64-bit unsigned integer. Your external function declaration specifies a PB UnsignedLong, which is a 32-bit unsigned integer, which will not work.

Since PB does not have a native UnsignedLongLong datatype, you should try using a PB LongLong, which is a 64-bit signed integer, instead.

Also, the next to last argument value (called cbDerivedKey in the documentation) is an input value that contains the length of the buffer that is to receive the derived key. You are supplying the value zero for this argument, which I do not believe is correct. If I'm reading your code correctly, this value should instead be 256.

Here is a link to this WinAPI function's documentation:


I hope this helps.

Best regards, John

  1. John Fauss
  2. Tuesday, 4 October 2022 02:29 AM UTC
Likely caused by using a PB Ulong instead of a PB LongLong as I explained above.
  1. Helpful 2
  1. Sim Joo Pee
  2. Tuesday, 4 October 2022 02:51 AM UTC
Thanks John. I got it worked already, but now is another error, telling me, "An invalid handle was specified". Do you know why? I know something wrong in BCryptOpenAlgorithmProvider calls. But, not sure how to solve it. ANy idea?

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))
  1. Helpful
  1. John Fauss
  2. Tuesday, 4 October 2022 04:06 AM UTC
1. I looked at how you were initializing the buffer (even though this line was commented in your post): //lblb_resultkey = Blob(Space(256), iEncoding) and that you had specified Ansi (1 character = 1 byte) encoding.

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!
  1. Helpful
There are no comments made yet.
  • Page :
  • 1

There are no replies made for this question yet.
However, you are not allowed to reply to this question.