繁体   English   中英

如何在 C/C++ 中获取 RSA 公钥模数和指数?

[英]How to fetch RSA public key modulus and exponent in C/C++?

我正在使用BCrypt Windows 库来处理我的应用程序中的 RSA 算法。

问题:我需要获取 RSA公钥模数和指数

在 C# 语言中,我使用 RSACryptoProvider class 来获取这些信息(ExportParameters 方法)。

在我的 C/C++ 应用程序中,BCrypt 似乎无法获取正确的数据......

到目前为止我所做的:

BOOL RSA_New(RSA_CTX_ST* rsa_ctx, const BYTE key_data[256], DWORD key_len, BOOL cipher)
{
    if (rsa_ctx == NULL || key_len != RSA_2048_BLOCK_SIZE)
    {
        return FALSE;
    }

    NTSTATUS status = STATUS_UNSUCCESSFUL;
    BCRYPT_ALG_HANDLE* algo_handle = NULL;
    BCRYPT_KEY_HANDLE* key_handle = NULL;

    algo_handle = malloc(sizeof(BCRYPT_ALG_HANDLE));
    key_handle = malloc(sizeof(BCRYPT_KEY_HANDLE));

    if (algo_handle == NULL || key_handle == NULL) goto END;

    //Creation handle Algo
    if (!NT_SUCCESS(status = BCryptOpenAlgorithmProvider(algo_handle, BCRYPT_RSA_ALGORITHM, NULL, 0)))
    {
        UTL_Trace("SEC", VRB_MAJOR, "RSA:Algorithm handle opening error");
        if (status == STATUS_INVALID_HANDLE)
        {
            UTL_Trace("SEC", VRB_INFO, "INVALID HANDLE");
        }
        if (status == STATUS_INVALID_PARAMETER)
        {
            UTL_Trace("SEC", VRB_INFO, "INVALID PARAMS");
        }
        goto END;
    }

    // Key pair generation
    if (!NT_SUCCESS(status = BCryptGenerateKeyPair(algo_handle, key_handle, 2048, 0)))
    {
        UTL_Trace("SEC", VRB_MAJOR, "RSA:Key pair generating error");
        if (status == STATUS_INVALID_HANDLE)
        {
            UTL_Trace("SEC", VRB_INFO, "INVALID HANDLE");
        }
        if (status == STATUS_INVALID_PARAMETER)
        {
            UTL_Trace("SEC", VRB_INFO, "INVALID PARAMS");
        }
        goto END;
    }

    BCRYPT_RSAKEY_BLOB my_rsa_blob;
    ULONG* pcbResult = NULL;
    pcbResult = calloc(1, sizeof(ULONG));
    if (pcbResult == NULL)
    {
        UTL_Trace("SEC", VRB_INFO, "Allocating RSA result error");
        goto END;
    }

    // Export parameters of keys
    if (!NT_SUCCESS(status = BCryptExportKey(key_handle, NULL, BCRYPT_RSAFULLPRIVATE_BLOB, &my_rsa_blob, sizeof(my_rsa_blob), pcbResult, 0)))
    {
        UTL_Trace("SEC", VRB_MAJOR, "RSA:Key pair exporting error");
        if (status == STATUS_INVALID_HANDLE)
        {
            UTL_Trace("SEC", VRB_INFO, "INVALID HANDLE");
        }
        if (status == STATUS_INVALID_PARAMETER)
        {
            UTL_Trace("SEC", VRB_INFO, "INVALID PARAMS");
        }
        goto END;
    }
    return TRUE;

END:
    if (algo_handle != NULL) free(algo_handle);
    if (key_handle != NULL) free(key_handle);

    return FALSE;
}

在 my_rsa_blob 中,我应该有模数和指数的大小,但不是它们的值......

有没有人可以解决这个问题?

事实上,我确实找到了解决方案!

通过调用BCryptExportKey ,变量 my_rsa_blob 应该是一个 PUCHAR 变量,它是一个指向字符串的指针。

有了这个,我在搜索时在 Microsoft Docs 中找到了一个链接......该链接显示了 PUCHAR 变量的设计方式:

https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-wcce/540b7b8b-2232-45c8-9d7c-af7a5d5218ed

之后,您可以尝试像这样操作数据:

static BOOL RSA_RecoverKeyInformation(RSA_CTX_ST* rsa_ctx, PUCHAR ptrKey)
{
    strncpy(rsa_ctx->key_information.header.Magic, ptrKey[0], 4);
    strncpy(rsa_ctx->key_information.header.BitLength, ptrKey[1 * 4], 4);
    strncpy(rsa_ctx->key_information.header.cbPublicExp, ptrKey[2 * 4], 4);
    strncpy(rsa_ctx->key_information.header.cbModulus, ptrKey[3 * 4], 4);
    strncpy(rsa_ctx->key_information.header.cbPrime1, ptrKey[4 * 4], 4);
    strncpy(rsa_ctx->key_information.header.cbPrime2, ptrKey[5 * 4], 4);

    size_t sizeOfModulus = rsa_ctx->key_information.header.cbModulus;
    size_t sizeOfExponent = rsa_ctx->key_information.header.cbPublicExp;

    rsa_ctx->key_information.keyExponent = malloc(sizeOfExponent);
    rsa_ctx->key_information.keyModulus = malloc(sizeOfModulus);
    if (rsa_ctx->key_information.keyExponent == NULL || rsa_ctx->key_information.keyModulus == NULL) goto END;

    strncpy(rsa_ctx->key_information.keyExponent, ptrKey[6 * 4], sizeOfExponent);
    strncpy(rsa_ctx->key_information.keyModulus, ptrKey[6 * 4 + sizeOfExponent], sizeOfModulus);

    return TRUE;

END:
    if (rsa_ctx->key_information.keyExponent != NULL) free(rsa_ctx->key_information.keyExponent);
    if (rsa_ctx->key_information.keyModulus != NULL) free(rsa_ctx->key_information.keyModulus);
    return FALSE;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM