我想对数据使用 AES 256 位加密,它由“MS_ENH_RSA_AES_PROV”提供。为此,当我尝试运行下面的代码时 pszProviderName=TEXT("MS_ENH_RSA_AES_PROV") 作为 CryptAcquireContext 的第三个参数,我得到类似错误号 80090019 的输出。此错误代码意味着“请求的提供程序不存在”。
#include <windows.h>
#include <Wincrypt.h>
#pragma comment(lib, "crypt32.lib")
void main(void)
{
// Handle for the cryptographic provider context.
HCRYPTPROV hCryptProv;
// The name of the container.
LPCTSTR pszContainerName = TEXT("MyFilter_Container");
// The name of the provider
LPCTSTR pszProviderName = TEXT("MS_ENH_RSA_AES_PROV");
// Begin processing. Attempt to acquire a context
if(CryptAcquireContext(
&hCryptProv,
pszContainerName,
pszProviderName,
PROV_RSA_FULL,
0))
{
_tprintf(TEXT("A crypto context acquired"));
// Release the CSP.
if(hCryptProv)
{
if(!(CryptReleaseContext(hCryptProv, 0)))
{
_tprintf(TEXT("Error during CryptReleaseContext."));
}
}
}
else
{
_tprintf(TEXT("An error occurred in the program. \n"));
_tprintf(TEXT("Error number %x.\n"), GetLastError());
exit(1);
}
}
如果我使用 pszProviderName=TEXT("Microsoft 增强型 RSA 和 AES 加密提供程序"), 它给出错误号 0x8009001b,这意味着“dwProvType 指定的提供程序类型与找到的提供程序类型不匹配,并且仅当 pszProviderName 指定实际的 CSP 名称时才会发生此错误”。以上错误号在 msdn 的 CryptAcquireContext 文档中指定。我不明白为什么会发生这种情况。如果此参数为 Null,则意味着使用默认 CSP,在这种情况下一切正常。我用的是windows7。此 CSP 不可用还是存在其他原因?请有人帮忙。谢谢。
最佳答案
我不确定这是否正是您所需要的,但请遵循 the example on MSDN ,您可以通过进行两项更改来使代码成功。
首先,MSDN says MS_ENH_RSA_AES_PROV
在 Windows XP 上的命名不同,应与 PROV_RSA_AES
提供程序类型一起使用,而不是与代码中的 PROV_RSA_FULL
一起使用。
其次,捕获初始错误并在必须创建新 key 容器的情况下重复获取操作。
以下是您根据MSDN示例改编的原始代码,在我的Windows 8系统上运行良好:
int _tmain(int argc, _TCHAR* argv[])
{
// Handle for the cryptographic provider context.
HCRYPTPROV hCryptProv;
// The name of the container.
LPCTSTR pszContainerName = TEXT("MyFilter_Container");
// Begin processing. Attempt to acquire a context
if(CryptAcquireContext(
&hCryptProv,
pszContainerName,
MS_ENH_RSA_AES_PROV,
PROV_RSA_AES,
0))
{
_tprintf(TEXT("A crypto context acquired"));
// Release the CSP.
if(hCryptProv)
{
if(!(CryptReleaseContext(hCryptProv, 0)))
{
_tprintf(TEXT("Error during CryptReleaseContext."));
}
}
}
else
{
DWORD dwError = GetLastError();
if (dwError == NTE_BAD_KEYSET)
{
if(CryptAcquireContext(
&hCryptProv,
pszContainerName,
MS_ENH_RSA_AES_PROV,
PROV_RSA_AES,
CRYPT_NEWKEYSET))
{
_tprintf(TEXT("A crypto context acquired"));
// Release the CSP.
if(hCryptProv)
{
if(!(CryptReleaseContext(hCryptProv, 0)))
{
_tprintf(TEXT("Error during CryptReleaseContext."));
}
}
}
else
{
_tprintf(TEXT("Unable to create a new key container. \n"));
_tprintf(TEXT("Error number %x.\n"), dwError);
exit(1);
}
}
else
{
_tprintf(TEXT("An error occurred in the program. \n"));
_tprintf(TEXT("Error number %x.\n"), dwError);
exit(1);
}
}
return 0;
}
一旦成功,您可以使用CryptGetProvParam
查找所使用的提供程序,在我的系统上提供
Microsoft 增强型 RSA 和 AES 加密提供程序
关于visual-c++ - 微软的 cryptoAPI "Microsoft Enhanced RSA and AES Cryptographic Provider"选择在 win7 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17382176/