首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从BCRYPT_SECRET_HANDLE中将共享密钥导出为字节数组

从BCRYPT_SECRET_HANDLE中将共享密钥导出为字节数组
EN

Stack Overflow用户
提问于 2016-06-30 14:37:24
回答 4查看 1.4K关注 0票数 3

我正在使用加密的下一代API (CNG)实现ECDHE。我成功生成了公钥和私钥。对于预共享密钥,我使用接口,它会返回预共享密钥秘密句柄 (BCRYPT_SECRET_HANDLE)。

如何从BCRYPT_SECRET_HANDLE中导出预共享密钥为字节数组?

EN

回答 4

Stack Overflow用户

发布于 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中的其他所有内容都是大端的)。因此,要在其他高端世界中使用该值,您需要对数据进行字节翻转。

票数 3
EN

Stack Overflow用户

发布于 2019-10-15 07:24:14

我需要执行以下操作,以下是执行关键项目的代码的摘录,您需要在此段之前导入私钥和公钥

代码语言:javascript
复制
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),我在我的生产代码中验证了它。如前所述,数组将被颠倒,您将需要颠倒字节数组来获取秘密。

票数 1
EN

Stack Overflow用户

发布于 2016-11-12 20:24:51

获得BCRYPT_SECRET_HANDLE后,您可以使用BCryptDeriveKey来获得实际的对称加密密钥。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38115602

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档