为什么我从 WinAPI/Crypt32 base64 编码函数中得到一些奇怪的换行符/换行符?

问题描述 投票:0回答:1

我有这段代码,应该使用 crypt32.dll/winapi 对字符串进行 Base64 编码:

#include <windows.h>
#include <stdio.h>
int encodeBase64(const char *input, char **output, DWORD *outputSize)
{
    HMODULE hCrypt32 = LoadLibrary("crypt32.dll");
    if (!hCrypt32) {
        return -2;
    }
    typedef BOOL(WINAPI *CryptBinaryToStringFunc)(const BYTE *, DWORD, DWORD, LPSTR, DWORD *);

    CryptBinaryToStringFunc cryptBinaryToString = (CryptBinaryToStringFunc)GetProcAddress(hCrypt32, "CryptBinaryToStringA");
    if (!cryptBinaryToString)
    {
        FreeLibrary(hCrypt32);
        return -3;
    }
    if (!cryptBinaryToString((const BYTE*)input, strlen(input), CRYPT_STRING_BASE64, NULL, outputSize))
    {
        FreeLibrary(hCrypt32);
        return -4;
    }
    *output = (char*)HeapAlloc(GetProcessHeap(), HEAP_NO_SERIALIZE, (*outputSize) * sizeof(char));
    if (!*output)
    {
        FreeLibrary(hCrypt32);
        return -5;
    }
    if (!cryptBinaryToString((const BYTE*)input, strlen(input), CRYPT_STRING_BASE64, *output, outputSize))
    {
        HeapFree(GetProcessHeap(), HEAP_NO_SERIALIZE, *output);
        FreeLibrary(hCrypt32);
        return -6;
    }
    FreeLibrary(hCrypt32);
    return 0;
}

int main()
{
    LPCSTR pszSource = "Man is distinguished, not only by his reason, but ...";
    char *pszDestination = NULL;
    DWORD nDestinationSize;
    int result = encodeBase64(pszSource, &pszDestination, &nDestinationSize);
    if (result == 0)
    {
        printf("Encoded string: '%s'", pszDestination);
        HeapFree(GetProcessHeap(), HEAP_NO_SERIALIZE, pszDestination);
    }
    else
    {
        printf("Error in Encoding...\n");
    }
    return 0;
}

输出应该是

Encoded string: 'TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCAuLi4='

但这就是我得到的:

Encoded string: 'TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1
dCAuLi4=
'

我的代码有问题吗?我想这是因为 base64 有 在其中,但显然不是,因为我通过使用适当的在线工具确认了它。

c winapi base64
1个回答
0
投票

我不会声称完全理解为什么,但是,为了删除输出中那些不需要的换行符,Base64编码的字符串,您可以将

CRYPT_STRING_NOCRLF
位添加到您的
dwFlags
参数中:

int encodeBase64(const char* input, char** output, DWORD* outputSize)
{
    DWORD cryptFlags = CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF; // Modify the flags
    HMODULE hCrypt32 = LoadLibrary("crypt32.dll");
    if (!hCrypt32) {
        return -2;
    }
    typedef BOOL(WINAPI* CryptBinaryToStringFunc)(const BYTE*, DWORD, DWORD, LPSTR, DWORD*);

    CryptBinaryToStringFunc cryptBinaryToString = (CryptBinaryToStringFunc)GetProcAddress(hCrypt32, "CryptBinaryToStringA");
    if (!cryptBinaryToString)
    {
        FreeLibrary(hCrypt32);
        return -3;
    }
    if (!cryptBinaryToString((const BYTE*)input, strlen(input), cryptFlags, NULL, outputSize))
    {
        FreeLibrary(hCrypt32);
        return -4;
    }
    *output = (char*)HeapAlloc(GetProcessHeap(), HEAP_NO_SERIALIZE, (*outputSize) * sizeof(char));
    if (!*output)
    {
        FreeLibrary(hCrypt32);
        return -5;
    }
    if (!cryptBinaryToString((const BYTE*)input, strlen(input), cryptFlags, *output, outputSize))
    {
        HeapFree(GetProcessHeap(), HEAP_NO_SERIALIZE, *output);
        FreeLibrary(hCrypt32);
        return -6;
    }
    FreeLibrary(hCrypt32);
    return 0;
}

输出:

Encoded string: 'TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCAuLi4='

来自文档

CRYPT_STRING_NOCRLF (0x40000000) – 不附加任何新行 编码字符串的字符。默认行为是使用 回车/换行 (CR/LF) 对 (0x0D/0x0A) 表示新的 线。

© www.soinside.com 2019 - 2024. All rights reserved.