如何使用diffie Hellman Sessionkey作为AES加密的密码

问题描述 投票:-1回答:1

我需要用c ++创建一个服务器和客户端,以便与AES_256交换Diffie Hellman公钥和加密,到目前为止,我正在使用MSDN样本来进行DH公钥的生成https://docs.microsoft.com/en-us/windows/win32/seccrypto/diffie-hellman-keys,并且在两端(客户端和服务器)与RC4都可以使用它但是在将样本转换为AES_256之后,我在客户端的EncryptDecrypt API上收到错误0x80090005(NET_BAD_DATA)。奇怪的部分是,如果客户端和服务器都在同一台计算机上运行(不是同一操作系统),结果是OK。服务器和客户端下面都添加了我用于将公钥转换为AES密码的代码。我的问题是:

  1. 我以这种方式(将公钥转换为AES)正确执行了操作,或者不是吗?

  2. 为什么结果在我的主机上可以,但是如果我将客户端移至其他VM发生错误(Net_BAD_DATA)?

*我从代码中删除了API结果测试部分,每个API调用均以原始代码进行了测试,任何API中均无错误。

任何帮助将不胜感激。

服务器端:

    CryptImportKey(hProvParty1,pbKeyBlob2,dwDataLen2,hPrivateKey1,0,&hSessionKey2);
    DWORD dwpassLength = 32;
    CryptAcquireContext(&hCryptProv,NULL,MS_ENH_RSA_AES_PROV,PROV_RSA_AES,0);
    CryptCreateHash(hCryptProv, CALG_SHA_256,0, 0,&hHash);
    CryptHashData(hHash,(BYTE*)hSessionKey2,dwpassLength,0);
    hKey = (HCRYPTKEY )(malloc(100));
    CryptDeriveKey(hCryptProv,CALG_AES_128,hHash,CRYPT_EXPORTABLE,&hKey);
    DWORD dwLength = sizeof(g_rgbData);
    CryptEncrypt(   hKey,0,TRUE,0,NULL, &dwLength,sizeof(g_rgbData));       
    DWORD dwpbdataLength = dwLength;
    BYTE * pbEncryptedData = (PBYTE)malloc(dwpbdataLength);     
    memcpy(pbEncryptedData, g_rgbData, sizeof(g_rgbData));
    dwLength = sizeof(g_rgbData);       
    CryptEncrypt(hKey,NULL, TRUE,0, pbEncryptedData,&dwLength,dwpbdataLength); 
    send(newsocket, (const char*)pbEncryptedData, dwLength, 0);

客户:

CryptImportKey(hProvParty1,pbKeyBlob2,dwDataLen2,hPrivateKey1,0,&hSessionKey2); 
BYTE * pbEncryptedData = (PBYTE)malloc(1024);
recv(ConnectSocket, (char *)pbEncryptedData, DEFAULT_BUFLEN, 0);//receiving encrypted data
DWORD dwpassLength = 32;
CryptAcquireContext(&hCryptProv,NULL,MS_ENH_RSA_AES_PROV,PROV_RSA_AES,0);
CryptCreateHash(hCryptProv,CALG_SHA_256,0,0,&hHash);
CryptHashData(hHash,(BYTE*)hSessionKey2,dwpassLength,0);
CryptDeriveKey(hCryptProv,CALG_AES_256,hHash,CRYPT_EXPORTABLE,&hKey);
CryptDecrypt(hKey,0,TRUE,0, pbEncryptedData,&dlength);
c++ aes diffie-hellman wincrypt
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.