0

有没有办法在不使用 .NET/COM 的情况下通过 Microsoft 的加密服务提供程序 (CSP) 生成强随机字节?例如,使用命令行或其他方式?

我想在 NodeJS 中更具体地使用它。

4

2 回答 2

1

是的,使用 Windows API。这是一个示例 C++ 代码:

#include "Wincrypt.h"

// ==========================================================================
HCRYPTPROV hCryptProv= NULL; // handle for a cryptographic provider context

// ==========================================================================
void DoneCrypt()
{
    ::CryptReleaseContext(hCryptProv, 0);
    hCryptProv= NULL;
}

// --------------------------------------------------------------------------
// acquire crypto context and a key ccontainer
bool InitCrypt()
{
    if (hCryptProv) // already initialized
        return true;

    if (::CryptAcquireContext(&hCryptProv        ,   // handle to the CSP
                              NULL               ,   // container name
                              NULL               ,   // use the default provider
                              PROV_RSA_FULL      ,   // provider type
                              CRYPT_VERIFYCONTEXT )) // flag values
    {
        atexit(DoneCrypt);
        return true;
    }

    REPORT(REP_ERROR, _T("CryptAcquireContext failed"));
    return false;
}

// --------------------------------------------------------------------------
// fill buffer with random data
bool RandomBuf(BYTE* pBuf, size_t nLen)
{
    if (!hCryptProv)
        if (!InitCrypt())
            return false;

    size_t nIndex= 0;

    while (nLen-nIndex)
    {
        DWORD nCount= (nLen-nIndex > (DWORD)-1) ? (DWORD)-1 : (DWORD)(nLen-nIndex);
        if (!::CryptGenRandom(hCryptProv, nCount, &pBuf[nIndex]))
        {
            REPORT(REP_ERROR, _T("CryptGenRandom failed"));
            return false;
        }

        nIndex+= nCount;
    }

    return true;
}
于 2012-11-10T17:16:53.427 回答
1

参考http://technet.microsoft.com/en-us/library/cc733055(v=ws.10).aspx

netsh nap client set csp name = <name> keylength = <keylength>

如果此命令对您有用,只需通过 nodejs 执行即可。(需要('child_process').exec)

于 2012-11-05T19:09:30.687 回答