C++ 打印数字签名信息

C++ 打印数字签名信息,c++,visual-c++,digital-signature,digital-certificate,C++,Visual C++,Digital Signature,Digital Certificate,正如在指定的标题中一样,我希望将数字签名信息打印到控制台。以下是我编写的代码: bool CheckDigSig(const std::wstring& filepath) { bool rval = false; DWORD dwEncoding = 0; DWORD dwContentType = 0; DWORD dwFormatType = 0; HCERTSTORE hStore = NULL; HCRYPTMSG hMsg =

正如在指定的标题中一样,我希望将数字签名信息打印到控制台。以下是我编写的代码:

bool CheckDigSig(const std::wstring& filepath)
{
    bool rval = false;

    DWORD dwEncoding = 0;
    DWORD dwContentType = 0;
    DWORD dwFormatType = 0;
    HCERTSTORE hStore = NULL;
    HCRYPTMSG hMsg = NULL;

    // Get message handle and store handle from the signed file.
    BOOL fResult = CryptQueryObject(CERT_QUERY_OBJECT_FILE,
        filepath.c_str(),
        CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED_EMBED,
        CERT_QUERY_FORMAT_FLAG_BINARY,
        0,
        &dwEncoding,
        &dwContentType,
        &dwFormatType,
        &hStore,
        &hMsg,
        NULL);
    if (!fResult)
        return false;

    DWORD singer_info_size = 0;
    // Get signer information size.
    fResult = CryptMsgGetParam(hMsg, CMSG_SIGNER_INFO_PARAM, 0, NULL, &singer_info_size);
    if (!fResult)
    {
        CryptMsgClose(hMsg);
        CertCloseStore(hStore, 0);
        return false;
    }

    // Allocate memory for signer information.
    std::vector<byte> signer_info_data(singer_info_size);
    PCMSG_SIGNER_INFO pSignerInfo = reinterpret_cast<PCMSG_SIGNER_INFO>(signer_info_data.data());

    // Get Signer Information.
    fResult = CryptMsgGetParam(hMsg, CMSG_SIGNER_INFO_PARAM, 0, (PVOID)pSignerInfo, &singer_info_size);
    if (fResult)
    {
        //pSignerInfo->Issuer;
        //pSignerInfo->SerialNumber;
    }

    CryptMsgClose(hMsg);
    CertCloseStore(hStore, 0);
    return rval;
}
bool CheckDigSig(const std::wstring&filepath)
{
bool-rval=false;
DWORD编码=0;
DWORD dwContentType=0;
DWORD dwFormatType=0;
HCERTSTORE hStore=NULL;
HCRYPTMSG hMsg=NULL;
//从签名文件中获取消息句柄和存储句柄。
BOOL fResult=CryptQueryObject(证书查询对象文件,
filepath.c_str(),
证书查询内容标志PKCS7签名嵌入,
证书\查询\格式\标志\二进制,
0,
&DWG编码,
&dwContentType,
&dwFormatType,
&商店,
&hMsg,
无效);
如果(!fResult)
返回false;
DWORD singer\u info\u size=0;
//获取签名者信息大小。
fResult=CryptMsgGetParam(hMsg、CMSG、签名者信息参数、0、NULL和singer信息大小);
如果(!fResult)
{
CryptMsgClose(hMsg);
CertCloseStore(hStore,0);
返回false;
}
//为签名者信息分配内存。
标准::矢量签名者信息数据(歌手信息大小);
PCMSG_SIGNER_INFO pSignerInfo=reinterpret_cast(SIGNER_INFO_data.data());
//获取签名者信息。
fResult=CryptMsgGetParam(hMsg、CMSG、签名者信息参数、0、(PVOID)pSignerInfo和singer信息大小);
if(fResult)
{
//pSignerInfo->发卡机构;
//pSignerInfo->SerialNumber;
}
CryptMsgClose(hMsg);
CertCloseStore(hStore,0);
返回rval;
}
我想在末尾打印这两个变量(现在推荐): pSignerInfo->发卡机构; pSignerInfo->SerialNumber

我不知道如何使它成为可读的格式,比如字符串、字节或字符数组。你能帮我一下吗?

这篇文章有你需要的代码。以下是其中的一小段:

    // Get Issuer name.
    if (!(CertGetNameString(pCertContext, 
                            CERT_NAME_SIMPLE_DISPLAY_TYPE,
                            CERT_NAME_ISSUER_FLAG,
                            NULL,
                            szName,
                            dwData)))
    // ...
显然,这里有更多的代码,涵盖了这个任务的各个方面。包括打印
序列号