【问题标题】:CryptImportKey fails on Windows 8CryptImportKey 在 Windows 8 上失败
【发布时间】:2015-05-28 20:12:05
【问题描述】:

使用 Crypto32 Windows,函数 CryptoImportKey 在 Windows 8.1 上失败,返回 ERROR_INVALID_PARAMETER。它适用于所有先前版本的 Windows。

有人有什么提示吗?

代码是:

if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, 0))
{
    dwResult = GetLastError();
    if (dwResult == NTE_BAD_KEYSET)
    {

        if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET))
        {
            dwResult = GetLastError();
            strAux.Format("Error [%x]: CryptAcquireContext() failed.",dwResult);
            AfxMessageBox(  strAux,  MB_OK);
            return;
        }
    } else {
        dwResult = GetLastError();
        strAux.Format("Error [0x%x]: CryptAcquireContext() SECOND failed.",dwResult);
        AfxMessageBox(  strAux,  MB_OK);
        return;
    }
}

if (pbBlob != NULL)
{  
    //Porto 02-07-2014
    *(DWORD *)(pbBlob + 0x14) = 0; // Set the packed key length to zero 

    if (!CryptImportKey(hProv, pbBlob, cbBlob, 0, 0, &hSessionKey))
    {
        dwResult = GetLastError();
        strAux.Format("Error [%x]: CryptImportKey() failed.Size: %d",dwResult,cbBlob);
        AfxMessageBox(  strAux,  MB_OK);
        return;
    }
} else { 
    if (!CryptImportKey(hProv, PrivateKeyWithExponentOfOne, sizeof(PrivateKeyWithExponentOfOne), 0, 0, &hKey))
    {
        strAux.Format("Error [%x]: CryptImportKey() PRIVATE failed.",dwResult);
        AfxMessageBox(  strAux,  MB_OK);
        return;
    }

【问题讨论】:

  • 请显示您正在使用的代码。尤其是打开容器的方式。

标签: windows winapi mscapi


【解决方案1】:

described here 存在错误。从 Windows 7 导出的密钥无法导入到 Windows 8.1。

该博客文章描述了一种解决方法。在 OPAQUEKEYBLOB 中,将偏移量 0x14 处的 DWORD 设置为零:

*(DWORD *)(lpBlob + 0x14) = 0; // Set the packed key length to zero 

这允许将 Windows 7 密钥导入 Windows 8.1。

【讨论】:

  • 它不适用于我的情况。我已经在同一个 Windows 8.1 系统中创建和导入密钥。谢谢。何塞·波尔图
猜你喜欢
  • 2015-06-19
  • 1970-01-01
  • 2014-11-15
  • 1970-01-01
  • 1970-01-01
  • 2015-12-13
  • 1970-01-01
  • 2013-03-27
  • 2017-10-06
相关资源
最近更新 更多