我正在使用加密的下一代API (CNG)实现ECDHE。我成功生成了公钥和私钥。对于预共享密钥,我使用接口,它会返回预共享密钥秘密句柄 (BCRYPT_SECRET_HANDLE)。
如何从BCRYPT_SECRET_HANDLE中导出预共享密钥为字节数组?
发布于 2019-05-30 20:52:07
从Windows10开始,你可以用BCRYPT_KDF_RAW_SECRET
调用BCryptDeriveKey()
。
所得到的密钥数据是原始的秘密。
注1: bcrypt.h表示此格式适用于"WINBLUE",如果我理解正确的话,它将是Windows8.1,但我收到了在Windows8.1和Windows Server2012 R2上使用此KDF类型的STATUS_NOT_SUPPORTED。然而,这在Windows 10上是有效的。)
Note2:我发现使用这种KDF类型返回的数据是小端的( BCrypt中的其他所有内容都是大端的)。因此,要在其他高端世界中使用该值,您需要对数据进行字节翻转。
发布于 2019-10-15 07:24:14
我需要执行以下操作,以下是执行关键项目的代码的摘录,您需要在此段之前导入私钥和公钥
DWORD bCryptStatus;
BCRYPT_SECRET_HANDLE secretHandle = NULL;
BCRYPT_KEY_HANDLE privateKeyHandle= NULL;
BCRYPT_KEY_HANDLE importedPublicKey = NULL;
BYTE *agreedSecret = NULL;
DWORD agreedSecretLength = 0;
//Import your keys here
//Generate the secret from the imported keys
bCryptStatus= BCryptSecretAgreement(privateKeyHandle, importedPublicKey, &secretHandle, 0);
//Now get the raw value of the secret agreement and copy it into an array
bCryptStatus= BCryptDeriveKey(
secretHandle, // Secret agreement handle
BCRYPT_KDF_RAW_SECRET, // Key derivation function (null terminated unicode string)
NULL, // KDF parameters
NULL, // Buffer that recieves the derived key
0, // Length of the buffer
&agreedSecretLength, // Number of bytes copied to the buffer
0); // Flags
agreedSecret = (PBYTE)MALLOC(agreedSecretLength);
if (NULL != agreedSecret)
{
_nCryptError = BCryptDeriveKey(
secretHandle, // Secret agreement handle
BCRYPT_KDF_RAW_SECRET, // Key derivation function (null terminated unicode string)
NULL, // KDF parameters
agreedSecret, // Buffer that recieves the derived key
agreedSecretLength, // Length of the buffer
&agreedSecretLength, // Number of bytes copied to the buffer
0); // Flags
}
//Free all the objects and the array when you are done, otherwise you will get memory leaks
if (NULL != importedPublicKey)
{
BCryptDestroyKey(importedPublicKey);
}
if (NULL != privateKeyHandle)
{
BCryptDestroyKey(privateKeyHandle);
}
if (NULL != secretHandle)
{
BCryptDestroySecret(secretHandle);
}
if (NULL != agreedSecret)
{
FREE(agreedSecret);
}
顺便说一句,如果你使用NCrypt,这也会起作用(NCryptDeriveKey),我在我的生产代码中验证了它。如前所述,数组将被颠倒,您将需要颠倒字节数组来获取秘密。
发布于 2016-11-12 20:24:51
获得BCRYPT_SECRET_HANDLE
后,您可以使用BCryptDeriveKey
来获得实际的对称加密密钥。
https://stackoverflow.com/questions/38115602
复制相似问题