Windows CNG自定义密钥存储提供程序

时间:2012-03-08 23:47:57

标签: .net cryptography cryptoapi cng

如何使用自己的密钥BLOB格式等在CNG中注册自定义密钥存储提供程序?我真正想要做的是提供在.NET中处理自定义CNG密钥BLOB格式的能力。我在CNG文档中读到它提供了添加第三方KSP的方法,但是找不到任何样本或教程如何做到这一点。

1 个答案:

答案 0 :(得分:1)

  

如何使用自己的密钥在CNG中注册自定义密钥存储提供程序   BLOB格式等?

由于您只想注册,我假设您已准备好自定义KSP,只需注册即可。无论如何,你可以以编程方式进行。

以下代码来自Cryptographic Provider Development Kit随附的Sample KSP (http://www.microsoft.com/en-us/download/details.aspx?id=30688

    void
RegisterProvider(
    void
    )
{
    NTSTATUS ntStatus = STATUS_SUCCESS;

    //
    // Make CNG aware that our provider
    // exists...
    //
    ntStatus = BCryptRegisterProvider(
                    SAMPLEKSP_PROVIDER_NAME,
                    0,                          // Flags: fail if provider is already registered
                    &SampleKSPProvider
                    );
    if (!NT_SUCCESS(ntStatus))
    {
        wprintf(L"BCryptRegisterProvider failed with error code 0x%08x\n", ntStatus);
    }

    //
    // Add the algorithm name to the priority list of the
    // symmetric cipher algorithm class. (This makes it
    // visible to BCryptResolveProviders.)
    //
    ntStatus = BCryptAddContextFunction(
                    CRYPT_LOCAL,                    // Scope: local machine only
                    NULL,                           // Application context: default
                    NCRYPT_KEY_STORAGE_INTERFACE,   // Algorithm class
                    NCRYPT_KEY_STORAGE_ALGORITHM,   // Algorithm name
                    CRYPT_PRIORITY_BOTTOM           // Lowest priority
                    );
    if ( !NT_SUCCESS(ntStatus))
    {
        wprintf(L"BCryptAddContextFunction failed with error code 0x%08x\n", ntStatus);
    }

    //
    // Identify our new provider as someone who exposes
    // an implementation of the new algorithm.
    //
    ntStatus = BCryptAddContextFunctionProvider(
                    CRYPT_LOCAL,                    // Scope: local machine only
                    NULL,                           // Application context: default
                    NCRYPT_KEY_STORAGE_INTERFACE,   // Algorithm class
                    NCRYPT_KEY_STORAGE_ALGORITHM,   // Algorithm name
                    SAMPLEKSP_PROVIDER_NAME,        // Provider name
                    CRYPT_PRIORITY_BOTTOM           // Lowest priority
                    );
    if ( !NT_SUCCESS(ntStatus))
    {
        wprintf(L"BCryptAddContextFunctionProvider failed with error code 0x%08x\n", ntStatus);
    }
}
相关问题