locked
How to use BCryptDeriveKeyPBKDF2 function.Need Example . RRS feed

  • Question

  • Please any one share the code if worked on BCryptDeriveKeyPBKDF2 function.

    Below are the callback which i am using in my code but for BCryptDeriveKeyPBKDF2 function i am not getting the success

    response.Please help me out.

    #include "pch.h" #include "Class1.h" #define WIN32_NO_STATUS #include <windows.h> #undef WIN32_NO_STATUS #include <winternl.h> #include <ntstatus.h> #include <winerror.h> #include <stdio.h> #include <sal.h> #include <windows.h> #include <bcrypt.h> using namespace FIFS_Support_BCryptDeriveKeyPBKDF2; using namespace Platform; Class1::Class1() { } void ReportError( _In_ uint64 dwErrCode ) { wprintf(L"Error: 0x%08x (%d)\n", dwErrCode, dwErrCode); } uint64 Class1::PeformKeyDerivation(_In_ int32 ArrayIndex) { uint64 Status; BCRYPT_ALG_HANDLE KdfAlgHandle = NULL; BCRYPT_KEY_HANDLE SecretKeyHandle = NULL; DWORD ResultLength = 0; PBYTE DerivedKey = NULL; DWORD DerivedKeyLength = 0; Status = BCryptOpenAlgorithmProvider( &KdfAlgHandle, // Alg Handle pointer BCRYPT_SHA256_ALGORITHM, // Cryptographic Algorithm name (null terminated unicode string) NULL, // Provider name; if null, the default provider is loaded 0); if (!BCRYPT_SUCCESS(Status)) { ReportError(Status); goto cleanup; } Status = BCryptGetProperty( KdfAlgHandle, BCRYPT_OBJECT_LENGTH, (PBYTE)&DerivedKeyLength, sizeof(DWORD), &ResultLength, 0); if (!BCRYPT_SUCCESS(Status)) { ReportError(Status); goto cleanup; } DerivedKey = (PBYTE)HeapAlloc(GetProcessHeap(), 0, DerivedKeyLength); Status = BCryptDeriveKeyPBKDF2( KdfAlgHandle, // Algorithm Handle (PBYTE)Secret, sizeof(Secret), // Size of the password in bytes (PBYTE)Salt, sizeof(Salt), // Size of the salt in bytes IterationCount, // Iteration count time DerivedKey, // Address of the buffer which recieves the derived bytes DerivedKeyLength, // Size of the buffer in bytes 0); if (!BCRYPT_SUCCESS(Status)) { ReportError(Status); goto cleanup; } cleanup: if (NULL != DerivedKey) { HeapFree(GetProcessHeap(), 0, DerivedKey); DerivedKey = NULL; } if (NULL != SecretKeyHandle) { Status = BCryptDestroyKey(SecretKeyHandle); if (!BCRYPT_SUCCESS(Status)) { ReportError(Status); } SecretKeyHandle = NULL; } if (NULL != KdfAlgHandle) { Status = BCryptCloseAlgorithmProvider(KdfAlgHandle, 0); if (!BCRYPT_SUCCESS(Status)) { ReportError(Status); } KdfAlgHandle = NULL; } return Status; }


    #pragma once

    namespace FIFS_Support_BCryptDeriveKeyPBKDF2
    {
    static const
    BYTE Secret[20] =
    {
    0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
    0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
    };

    static
    BYTE Salt[] =
    {
    0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
    };

    static
    ULONGLONG IterationCount = 12000;

        public ref class Class1 sealed
        {
        public:
            Class1();

    public:
    uint64 PeformKeyDerivation(int32 ArrayIndex);
        };
    }
    Friday, March 31, 2017 1:58 PM

All replies

  • Have you tried to specify BCRYPT_ALG_HANDLE_HMAC_FLAG instead of zero in BCryptOpenAlgorithmProvider?

    • Proposed as answer by Baron Bi Monday, April 3, 2017 6:53 AM
    Friday, March 31, 2017 6:16 PM
  • Hi JYOTIJILU,

    Thank you for posting here.

    According to your question is more related to C++, I will move it to Visual C++ forum for suitable support.

    The Visual C# discuss and ask the C# programming language, IDE, libraries, samples and tools.

    If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution.

    Thanks for your understanding and cooperation.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 3, 2017 3:19 AM
  • Hi JYOTIJULU,

    thanks for posting here.

    >>How to use BCryptDeriveKeyPBKDF2 function.Need Example .

    Could you please show us the error code returned by BCryptDeriveKeyPBKDF2?

    According to this document , when you use the default Microsoft algorithm provider, any hashing algorithm opened by using the  BCRYPT_ALG_HANDLE_HMAC_FLAG flag can be used. And only algorithms that implement the BCRYPT_IS_KEYED_HASH  property can be used to populate this parameter.

    Hope this could be help of you.

    Best Regards,

    Sera Yu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Baron Bi Wednesday, May 3, 2017 7:34 AM
    Monday, April 3, 2017 7:08 AM