OK-- thanks to Mike, I was able to get Wincrypt to generate a Diffie-Hellman keypair. I figured out out to export the public key, and how to import the other party's public key. According to the docs, upon import of the other party's public key, the shared secret has been computed. Great.
I now need to get ahold of that shared secret, but I don't think its possible. Simply calling CryptExportKey
with a type of PLAINTEXTKEYBLOB
fails unless I call CryptSetKeyParam
to change the algorithm id from CALG_AGREEDKEY_ANY
to something... else. But I don't want something else, I want the shared secret. The API, however, seems designed to discourage this.
Any ideas out there? I should note that the problem here is that I'm only writing one side of an implementation of WiFi Protected Setup. So the protocol is defined for me, and the other party is not giving me HCRYPTKEYs.
This looks like what you need... from: http://msdn.microsoft.com/en-us/library/aa381969(VS.85).aspx
To import a Diffie-Hellman public key and calculate the secret session key
- Call the
CryptAcquireContext
function to get a handle to the Microsoft Diffie-Hellman Cryptographic Provider. - Create a Diffie-Hellman key by calling the
CryptGenKey
function to create a new key, or by calling theCryptGetUserKey
function to retrieve an existing key. - To import the Diffie-Hellman public key into the CSP, call the
CryptImportKey
function, passing a pointer to the public key BLOB in thepbData
parameter, the length of the BLOB in thedwDataLen
parameter, and the handle to the Diffie-Hellman key in thehPubKey
parameter. This causes the calculation,(Y^X) mod P
, to be performed, thus creating the shared, secret key and completing the key exchange. This function call returns a handle to the new, secret, session key in thehKey
parameter. - At this point, the imported Diffie-Hellman is of type
CALG_AGREEDKEY_ANY
. Before the key can be used, it must be converted into a session key type. This is accomplished by calling theCryptSetKeyParam
function withdwParam
set toKP_ALGID
and withpbData
set to a pointer to aALG_ID
value that represents a session key, such asCALG_RC4
. The key must be converted before using the shared key in theCryptEncrypt
orCryptDecrypt
function. Calls made to either of these functions prior to converting the key type will fail. - The secret session key is now ready to be used for encryption or decryption.
- When the key is no longer needed, destroy the key handle by calling the
CryptDestroyKey
function.
来源:https://stackoverflow.com/questions/87694/im-using-wincrypt-for-diffie-hellman-can-i-export-the-shared-secret-in-plain