0

使用 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;
    }
4

1 回答 1

0

这里有一个描述的错误。从 Windows 7 导出的密钥无法导入到 Windows 8.1。

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

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

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

于 2015-05-28T23:28:03.597 回答