gpt4 book ai didi

c# - 在 c# 中等效于 'Base64' 在 c 编程中生成的加密字节的编码字符串将在 URL 查询字符串中发送

转载 作者:行者123 更新时间:2023-11-28 06:14:59 25 4
gpt4 key购买 nike

我使用了来自网络的代码。但是在c程序中生成的结果与c#编码(ASCII、UTF-8等)不匹配。

在 c 中处理字符以克服此问题的最佳方法是什么?

这是基于 windows 的 exe。

对于文本“Í£kúæ›Ì”

in VC I get Base64 Encoded string as "zaNr+uabzADMzMzMpGICvnj/GAA2IUEAAQAAAA=="

但在 C# 中,我根据编码类型得到不同的结果

UTF-8 = > "K0FNMEFvdy1rK0FQb0E1aUE2QU13LQ=="
UTF-7 = > "w43Co2vDusOm4oC6w4w="

等等……C# 中的哪种编码/无编码会得到与 C 程序相同的结果。

#include <stdafx.h>
#include <locale.h>
#include "base64.h"

static const unsigned char pr2six[256] =
{
/* ASCII table */
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
};

int Base64decode_len(const char *bufcoded)
{
int nbytesdecoded;
register const unsigned char *bufin;
register int nprbytes;

bufin = (const unsigned char *) bufcoded;
while (pr2six[*(bufin++)] <= 63);

nprbytes = (bufin - (const unsigned char *) bufcoded) - 1;
nbytesdecoded = ((nprbytes + 3) / 4) * 3;

return nbytesdecoded + 1;
}

int Base64decode(char *bufplain, const char *bufcoded)
{
int nbytesdecoded;
register const unsigned char *bufin;
register unsigned char *bufout;
register int nprbytes;

bufin = (const unsigned char *) bufcoded;
while (pr2six[*(bufin++)] <= 63);
nprbytes = (bufin - (const unsigned char *) bufcoded) - 1;
nbytesdecoded = ((nprbytes + 3) / 4) * 3;

bufout = (unsigned char *) bufplain;
bufin = (const unsigned char *) bufcoded;

while (nprbytes > 4) {
*(bufout++) =
(unsigned char) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4);
*(bufout++) =
(unsigned char) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2);
*(bufout++) =
(unsigned char) (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]);
bufin += 4;
nprbytes -= 4;
}

/* Note: (nprbytes == 1) would be an error, so just ingore that case */
if (nprbytes > 1) {
*(bufout++) =
(unsigned char) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4);
}
if (nprbytes > 2) {
*(bufout++) =
(unsigned char) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2);
}
if (nprbytes > 3) {
*(bufout++) =
(unsigned char) (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]);
}

*(bufout++) = '\0';
nbytesdecoded -= (4 - nprbytes) & 3;
return nbytesdecoded;
}

static const char basis_64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

int Base64encode_len(int len)
{
return ((len + 2) / 3 * 4) + 1;
}

int Base64encode(char *encoded, const char *string, int len)
{
int i;
char *p;

p = encoded;
for (i = 0; i < len - 2; i += 3) {
*p++ = basis_64[(string[i] >> 2) & 0x3F];
*p++ = basis_64[((string[i] & 0x3) << 4) |
((int) (string[i + 1] & 0xF0) >> 4)];
*p++ = basis_64[((string[i + 1] & 0xF) << 2) |
((int) (string[i + 2] & 0xC0) >> 6)];
*p++ = basis_64[string[i + 2] & 0x3F];
}
if (i < len) {
*p++ = basis_64[(string[i] >> 2) & 0x3F];
if (i == (len - 1)) {
*p++ = basis_64[((string[i] & 0x3) << 4)];
*p++ = '=';
}
else {
*p++ = basis_64[((string[i] & 0x3) << 4) |
((int) (string[i + 1] & 0xF0) >> 4)];
*p++ = basis_64[((string[i + 1] & 0xF) << 2)];
}
*p++ = '=';
}

*p++ = '\0';
return p - encoded;
}


int main() {
char mysrc[] = "Hello world...";

char mysrc[] = "Í£kúæ›Ì";
char myb64[1024] = "";
char mydst[1024] = "";

//Base64encode(myb64, mysrc, 28);
Base64encode(myb64, mysrc, strlen(mysrc); //corrected
printf("The string\n[%s]\nencodes into base64 as:\n[%s]\n", mysrc, myb64);
printf("\n");
Base64decode(mydst, myb64);
printf("The string\n[%s]\ndecodes from base64 as:\n[%s]\n", myb64, mydst);

return 0;
}

最佳答案

您可以尝试使用 Encoding.GetEncoding("ISO-8859-1"),所以

Encoding encoding = Encoding.GetEncoding("ISO-8859-1");
string base64Encoded = Convert.ToBase64String(encoding.GetBytes("Í£kúæ›Ì"));

请注意,一般来说,您的问题是一个非常复杂的问题...参见示例 https://gcc.gnu.org/onlinedocs/cpp/Character-sets.html gcc关于源文件编码的页面。

请注意,此响应通常是错误的...因为问题非常复杂。 C/C++ 中的 char* 更类似于未编码的字节数组,而不是字符串。C 源文件中包含的字符串常量("something")是按源文件的编码方式编码的字节数组。在 Visual Studio 中,您可以通过执行文件 -> 另存为来选择它,然后单击保存按钮的小箭头,使用编码保存。例如:

printf("%d", strlen("è"));

如果文件的编码是 Windows-1252,将打印 1,如果文件的编码是 utf-8,将打印 2

所以如果你想在 C# 中以相同的方式编码,你必须执行 Encoding.GetEncoding("encodingName")(或者 Encoding.UTF8 如果 C/C++ 文件是 UTF8 编码的)

但这仅适用于字符串常量!

例如,如果您执行 scanf("%s", str)(因此您从控制台接受一个字符串),str 中字节的编码> 将是控制台的编码。如果您从 Windows 控件中获取字符串,则编码将是 Windows 的默认编码(因此在 C# 中为 Encoding.Default)。如果您读取外部文件,则 char* 将是文件的字节,因此 char* 的编码将是文件的编码。

关于c# - 在 c# 中等效于 'Base64' 在 c 编程中生成的加密字节的编码字符串将在 URL 查询字符串中发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30504029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com