本文介绍了如何导出Diffie-Hellman共享密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用以下方法导出DH公钥和随机私钥:
使用1024位MODP组2:
G为2,P为:
I use the following method to export DH public key and random private key:
Using 1024 bit of MODP Group 2:
The G is 2 andthe P is:
static BYTE G_Prime[]={
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
0x49,0x28,0x66,0x51,0xEC,0xE6,0x53,0x81,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
};
PublicKey生成的方法:
PublicKey generated method:
struct DH_Struct
{
HCRYPTPROV hProv;
HCRYPTKEY hKey;
HCRYPTKEY hSessionKey;
BYTE G[128];
BYTE P[128];
struct _PublicBlob
{
BLOBHEADER header;
DHPUBKEY DhPubStruct;
BYTE PublicKey[128];
}PublicKey;
struct _PrivateBlob
{
BLOBHEADER header;
DHPUBKEY DhPubStruct;
BYTE P[128];
BYTE G[128];
BYTE PrivateKey[128];
}PrivateBlob;
BYTE RemotePublickKey[128];
};
void GetDHPublic(DH_Struct& ret)
{
HCRYPTPROV hProv;
BOOL blRet= CryptAcquireContextW(&hProv,
NULL, MS_ENH_DSS_DH_PROV,
PROV_DSS_DH, CRYPT_VERIFYCONTEXT);
int bitlength=128*8;
//DHKEYSIZE << 16
HCRYPTKEY hCryptKey;
blRet=CryptGenKey(hProv, CALG_DH_EPHEM,
bitlength << 16 |
CRYPT_EXPORTABLE | CRYPT_PREGEN,
&hCryptKey);
DATA_BLOB P={128,ret.P};
DATA_BLOB G={128,ret.G};
blRet=CryptSetKeyParam(hCryptKey,KP_P,(BYTE*)&P,0);
int _n1=GetLastError();
blRet=CryptSetKeyParam(hCryptKey,KP_G,(BYTE*)&G,0);
int _n=GetLastError();
blRet=CryptSetKeyParam(hCryptKey,KP_X,NULL,0);
DWORD dwPublicKey=0; //value=144;
blRet= CryptExportKey(
hCryptKey, NULL,
PUBLICKEYBLOB, 0, NULL, &dwPublicKey);
blRet= CryptExportKey(
hCryptKey, NULL,
PUBLICKEYBLOB, 0, (BYTE*)&ret.PublicKey, &dwPublicKey);
DWORD dwPrivateKey=0; //value=400;
blRet= CryptExportKey(
hCryptKey, NULL,
PRIVATEKEYBLOB, 0, NULL, &dwPrivateKey);
blRet= CryptExportKey(
hCryptKey, NULL,
PRIVATEKEYBLOB, 0, (BYTE*)&ret.PrivateBlob, &dwPrivateKey);
}
假设remotePublicKey是另一个客户生成的公钥:
Assuming remotePublicKey the public key generated by another client:
BYTE remotePublicKey[128];
导入此remotePublicKey:
Importing this remotePublicKey:
void ImportKey(DH_Struct& ret)
{
struct _MyStruct
{
PUBLICKEYSTRUC publicHeader;
DHPUBKEY publicKey;
byte data[128];
};
_MyStruct pkg;
pkg.publicHeader.bType=PUBLICKEYBLOB;
pkg.publicHeader.bVersion=0x02;
pkg.publicHeader.reserved=0;
pkg.publicHeader.aiKeyAlg=CALG_DH_EPHEM;
pkg.publicKey.bitlen=1024;
pkg.publicKey.magic=0x31484400; //DH1
memcpy(pkg.data,ret.RemotePublickKey,128);
int importSize=sizeof(_MyStruct);
HCRYPTKEY hSession;
BOOL blRet=CryptImportKey(ret.hProv,
(BYTE*)&pkg,importSize,
ret.hKey,0,&hSession);
ret.hSessionKey=hSession;
}
但是我想获得DH共享密钥,请帮助我,谢谢
But I want to get DH shared key , help me, thank you
推荐答案
这篇关于如何导出Diffie-Hellman共享密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!