Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C++
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 generated method:

C++
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);

}

Assuming remotePublicKey the public key generated by another client:

C++
BYTE remotePublicKey[128];


Importing this remotePublicKey:

C++
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;
}

But I want to get DH shared key , help me, thank you
Posted
Updated 13-Apr-14 17:57pm
v7
Comments
Sergey Alexandrovich Kryukov 13-Apr-14 23:16pm    
What do you mean by "export"? Export where? Why? Do you understand the concept of "private" in "private key"?
—SA
dyf7970268 13-Apr-14 23:51pm    
Thank you for your answer.
I'm sorry, I described is wrong, this is the result of Google Translate, and I describe in detail, which is Google translation.
I need to export the Diffie-Hellman shared key .
There are two users A and B.
G and P are known between 128 byte value. P is a large prime number, G is an arbitrary value.
A randomly generates a value Xa, B generates a random value Xb, Xa and Xb are confidential, and only two of them know.
A calculation Ya = G ^ Xa mod P.
B calculated Yb = G ^ Xb mod P.
The two sides exchanged value Ya and Yb.
A case can be calculated key Ka = Yb ^ Xa, B can be calculated key Kb = Ya ^ Xb mod P;
According to DH algorithms can know Ka equal Kb.
Ka and Kb are shared key.
When I was a user A, I want to export is the Ka value.
Sergey Alexandrovich Kryukov 14-Apr-14 0:22am    
Still, my question wasn't answered... Anyway, if you "described it wrong", better use "Improve question" above and put it all in one clear question.
—SA
[no name] 16-Apr-14 19:53pm    
Why would you want to do this?

The whole point of DH is for two parties to agree upon a shared key for a single session.
New session = new key.

Exporting the key defeats the purpose of DH. Also, the exported key quickly becomes useless.

Sorry, but what you ask makes no sense.
manoranjan 18-Apr-14 16:30pm    
You don't export Ka or Kb. Ka = Kb. As A, you will use Ka at your end to encrypt dsta to send it to B. B decrypts it using Kb. However, you have to export Ya and send it to B.

Do you want to know how you can use shared key (hSession in your ImportKey function) for encrypting data or create MAC?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900