NCryptFinalizeKey is throwing error -2146893808

Munagala, Rekha 21 Reputation points
2022-04-15T13:21:14+00:00

I wrote a simple program like following. I am getting error if adding NCRYPT_MACHINE_KEY_FLAG
The error is :NCryptFinalizeKey Response Code: -2146893808

NCRYPT_PROV_HANDLE prov = NULL;
NCRYPT_KEY_HANDLE key = NULL;
DWORD keyLength = 2048;
DWORD blobLen = 0x1000;
BYTE blobexport[0x1000] = {0};
DWORD policy = NCRYPT_ALLOW_EXPORT_FLAG | NCRYPT_ALLOW_PLAINTEXT_EXPORT_FLAG;
int errCode = 0;
errCode = NCryptOpenStorageProvider(&prov, MS_KEY_STORAGE_PROVIDER, 0) ;
qDebug()<<"NCryptOpenStorageProvider "<<" Response Code:"<<errCode<<endl;
errCode = NCryptCreatePersistedKey(prov, &key, NCRYPT_RSA_ALGORITHM, L"persist", 0, NCRYPT_MACHINE_KEY_FLAG ) ;
qDebug()<<"NCryptCreatePersistedKey "<<" Response Code:"<<errCode<<endl;
errCode = NCryptSetProperty(key, NCRYPT_LENGTH_PROPERTY, (PBYTE)(&keyLength), sizeof(keyLength), NCRYPT_PERSIST_FLAG) ;
qDebug()<<"NCryptSetProperty "<<" Response Code1:"<<errCode<<endl;
errCode = NCryptSetProperty(key, NCRYPT_EXPORT_POLICY_PROPERTY, (PBYTE)(&policy), sizeof(policy), NCRYPT_PERSIST_FLAG);
qDebug()<<"NCryptSetProperty "<<" Response Code2:"<<errCode<<endl;
errCode = NCryptFinalizeKey(key, 0);
qDebug()<<"NCryptFinalizeKey "<<" Response Code:"<<errCode<<endl;

Windows development | Windows API - Win32
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jeanine Zhang-MSFT 11,356 Reputation points Microsoft External Staff
    2022-04-18T07:50:18.63+00:00

    Hi,

    Welcome to Microsoft Q&A!

    error -2146893808: (error code 0x80090010) Access Denied.

    I suggest you should try to run as administrator.

    Best Regards,

    Jeanine


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.