Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSDN C example program for signing a hash and verifying the hash signature doesn't work

I've tried out the sample program at MSDN that demonstrates how a hash can be signed and a hash signature verified. But when I execute the program, I get the following output:

CSP context acquired.
An error occurred in running the program.
Error during CryptGetUserKey for signkey.
Error number 8009000d.
Program terminating.

8009000d is the numeric error code for NTE_NO_KEY. Does anyone have an idea why the program fails in this way? I'm on Windows 7.

like image 232
Matthias Boehm Avatar asked Oct 15 '25 04:10

Matthias Boehm


1 Answers

Apparently there is no key in the context you've aquired.

You can change slightly this example, by combining it with another microsoft example one about context acqusition and key generation. You have simply to add code for generateing keys if these are missing:

...
if (CryptGetUserKey(hProv,  AT_SIGNATURE, &hKey)) {  // if block unchanged
    printf("The signature key has been acquired. \n");
}
else if (GetLastError() == NTE_NO_KEY) {             // insert processing of missing keys 
    if (CryptGenKey(                                
        hProv,
        AT_SIGNATURE,
        0,
        &hKey)) {
        printf("Created a signature key pair.\n");
    }
    else {
        MyHandleError("Error during creation of new signature key.");
    }
}
else {                      // The original else part will handle other errors
    MyHandleError("Error during CryptGetUserKey for signkey.");
}
like image 185
Christophe Avatar answered Oct 17 '25 18:10

Christophe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!