0

我有下面的代码。我正在使用 char * 为 diffie-hellman 算法设置一个素数。设置素数后,我得到了错误的数据。我在哪里做错了?我在此链接中遵循了相同的示例。 https://msdn.microsoft.com/en-us/library/aa381969(VS.85).aspx#exchangeing_diffie-hellman_keys

使用 wincrypt 在 diffie-hellman 中设置素数的正确方法是什么?

#define DHKEYSIZE 1024
int fld_sz = 256;
BYTE* g_rgbPrime = new BYTE[DHKEYSIZE/8];   
char * prime = "A1BD60EBD2D43C53FA78D938C1EF8C9AD231F9862FC402739302DEF1B6BEB01E5BE59848A04C48B0069A8FB56143688678F7CC1097B921EA3E13E1EF9B9EB5381BEFDE7BBF614C13827493A1CA31DA76B4083B62C5073451D6B1F06A2F1049C291464AC68CBB2F69474470BBAD374073392696B6447C82BF55F20B2D015EB97B";
string s_prime(prime, fld_sz);
vector<std::string> res;
// split the string two charactes for converting into hex format
for (size_t i = 0; i < fld_sz; i += 2)
    res.push_back(s_prime.substr(i, 2));
for(int i = 0; i < res.size(); i++) {
    BYTE b = static_cast<BYTE>(std::stoi(res[i], 0, 16));
    g_rgbPrime[i] = b;
}
BYTE g_rgbGenerator[128] = 
{
    0x02
};
BOOL fReturn;
HCRYPTPROV hProvParty1 = NULL; 
HCRYPTPROV hProvParty2 = NULL; 
CRYPT_DATA_BLOB P;
CRYPT_DATA_BLOB G;
HCRYPTKEY hPrivateKey1 = NULL;
HCRYPTKEY hPrivateKey2 = NULL;
PBYTE pbKeyBlob1 = NULL;
PBYTE pbKeyBlob2 = NULL;
HCRYPTKEY hSessionKey1 = NULL;
HCRYPTKEY hSessionKey2 = NULL;
PBYTE pbData = NULL;

/************************
Construct data BLOBs for the prime and generator. The P and G 
values, represented by the g_rgbPrime and g_rgbGenerator arrays 
respectively, are shared values that have been agreed to by both 
parties.
************************/
P.cbData = DHKEYSIZE / 8;
P.pbData = (BYTE*)(g_rgbPrime);

G.cbData = DHKEYSIZE / 8;
G.pbData = (BYTE*)(g_rgbGenerator);

/************************
Create the private Diffie-Hellman key for party 1. 
************************/
// Acquire a provider handle for party 1.
fReturn = CryptAcquireContext(
    &hProvParty1, 
    NULL,
    MS_ENH_DSS_DH_PROV,
    PROV_DSS_DH, 
    CRYPT_VERIFYCONTEXT);
if(!fReturn)
{
    goto ErrorExit;
}

// Create an ephemeral private key for party 1.
fReturn = CryptGenKey(
    hProvParty1, 
    CALG_DH_EPHEM, 
    DHKEYSIZE << 16 | CRYPT_EXPORTABLE | CRYPT_PREGEN,
    &hPrivateKey1);
if(!fReturn)
{
    goto ErrorExit;
}

// Set the prime for party 1's private key.
fReturn = CryptSetKeyParam(
    hPrivateKey1,
    KP_P,
    (PBYTE)&P,
    0);
if(!fReturn)
{
    std::cout << GetLastError() << endl;
    goto ErrorExit;
}

// Set the generator for party 1's private key.
fReturn = CryptSetKeyParam(
    hPrivateKey1,
    KP_G,
    (PBYTE)&G,
    0);
if(!fReturn)
{
    std::cout << GetLastError() << endl;
    goto ErrorExit;
}

提前致谢。

更新 1:感谢@RbMm,我能够设置素数。问题在于 DHKEYSize。但是我在设置KP_X时遇到错误。更新了上面的代码以反映新代码。

在这里,我将字符串转换为十六进制字节数组。

4

1 回答 1

1

素数KP_P(和KP_G)的大小和 DH 密钥大小硬连接。必须是cbKey == 8*cbP。例如Diffie-Hellman Client Code for Creating the Master Key

cbP * 8如果使用where cbPprime 的大小,则作为密钥大小P。在您的链接中也P.cbData = DHKEYSIZE/8;

同样在代码中,而不是硬代码大小P(and G),您可以在运行时获取它:

ULONG dwDataLen;
CryptGetKeyParam(hPrivateKey1, KP_P, 0, &(dwDataLen = 0), 0);
CryptGetKeyParam(hPrivateKey1, KP_G, 0, &(dwDataLen = 0), 0);

并且您可以确定密钥大小dwDataLen == DHKEYSIZE / 8在哪里。DHKEYSIZE

因为您使用 512 作为密钥大小,所以 和 的数据长度P必须G512/8=64. 但您使用 256(代表P)和 1(代表G)。作为结果和错误。

于 2017-08-28T22:16:17.437 回答