gpt4 book ai didi

c# - 将 C++ 方法转换为 C#

转载 作者:行者123 更新时间:2023-11-28 04:32:44 25 4
gpt4 key购买 nike

我有一个解密 base64 的方法,我需要将它转换为 C#,我尝试使用 C# 的 Convert.FromBase64String 和我在此处查看的其他一些提示,但最终字符串不匹配c++ 上的结果。

有人可以帮助理解这种和平的 C++ 代码吗?

示例输入:lWXtYpNpwBBXoLmcuktDqg==

预期输出:AC0105E92ED496ACD373D7496B76C1C8

使用 Convert.FromBase64 的输出:95-65-ED-62-93-69-C0-10-57-A0-B9-9C-BA-4B-43-AA

static BYTE BackFrom64[]={
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0x3e,0xff,0xff,0xff,0x3f,
0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,
0x3c,0x3d,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0x00,0x01,0x02,0x03,0x04,0x05,0x06,
0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,
0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0x16,
0x17,0x18,0x19,0xff,0xff,0xff,0xff,0xff,
0xff,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,
0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,
0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,
0x31,0x32,0x33,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff

};

static BYTE * TabBase64=(BYTE *)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";


int FromBase64(BYTE *Output,BYTE *Input,int BufSize,int MaxSize) {

int xut,txut;
int i,t,k,shift;
BYTE c;
dWORD dr,dr1;

xut=txut=t=0;
dr=0L;
for(i=0; i < BufSize; ++i) {
c=*Input++;
if(c == '=') {
break;
}
if((c=BackFrom64[c]) == 0xff) {
continue;
}
dr1=(dWORD)c;
shift=(32-(6*(t+1)));
dr1<<=shift;
dr|=dr1;
t++;
if(t == 4) {
t=0;
for(k=0; k < 3; ++k) {
dr1=dr;
dr1>>=(32-(8*(k+1)));
dr1&=0xff;
*Output++=(BYTE) dr1;
if(++txut >= MaxSize) {
return(-1);
}
}
dr=0;
}
}
if(t > 0) {
for(k=0; k < (t-1); ++k) {
dr1=dr;
dr1>>=(32-(8*(k+1)));
dr1&=0xff;
*Output++=(BYTE) dr1;
if(++txut >= MaxSize) {
return(-1);
}
}
}
return(txut);
}

最佳答案

接下来是一个非常简单的转换,只需最少的工作。

  • 类型已更改为 C# 类型
  • 指针增量已更改为数组索引

唯一的其他变化是将未提供的 BackFrom64 数组计算为 TabBase64 数组的倒数,并为提供的输入和输出添加单元测试。如果它做了标准算法以外的事情,那么我会整理计数器,其中一些是多余的(可能 iiInput 都可以替换为 foreach(var c in input)

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace SO
{
[TestClass]
public class UnitTest1
{
private const string TabBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
private static readonly byte[] BackFrom64 = CreateBackFrom64();

private static byte[] CreateBackFrom64()
{
var backFrom64 = new byte[128];

for (var i = 0; i < backFrom64.Length; ++i)
backFrom64[i] = 0xff;

for (byte i = 0; i < TabBase64.Length; i++)
{
var c = TabBase64[i];
backFrom64[c] = i;
}

return backFrom64;
}

int FromBase64(byte[] Output, string Input, int BufSize, int MaxSize)
{
int xut, txut;
int i, t, k, shift;
byte c;
int dr, dr1;

xut = txut = t = 0;
dr = 0;

var iInput = 0;
var iOutput = 0;

for (i = 0; i < BufSize; ++i)
{
c = (byte)Input[iInput++];
if (c == '=')
{
break;
}
if ((c = BackFrom64[c]) == 0xff)
{
continue;
}
dr1 = c;
shift = (32 - (6 * (t + 1)));
dr1 <<= shift;
dr |= dr1;
t++;
if (t == 4)
{
t = 0;
for (k = 0; k < 3; ++k)
{
dr1 = dr;
dr1 >>= (32 - (8 * (k + 1)));
dr1 &= 0xff;
Output[iOutput++] = (byte)dr1;
if (++txut >= MaxSize)
{
return (-1);
}
}
dr = 0;
}
}
if (t > 0)
{
for (k = 0; k < (t - 1); ++k)
{
dr1 = dr;
dr1 >>= (32 - (8 * (k + 1)));
dr1 &= 0xff;
Output[iOutput++] = (byte)dr1;
if (++txut >= MaxSize)
{
return (-1);
}
}
}
return (txut);
}
[TestMethod]
public void TestMethod1()
{
var input = @"lWXtYpNpwBBXoLmcuktDqg==";
var expectedOutput = "AC0105E92ED496ACD373D7496B76C1C8";

var converted = new byte[(input.Length * 2) / 3];

var result = FromBase64(converted, input, input.Length, int.MaxValue);

Assert.AreNotEqual(-1, result, "result is valid");

Assert.AreEqual(
BitConverter.ToString(converted).Replace("-",""),
BitConverter.ToString(Convert.FromBase64String(input)).Replace("-",""),
"Supplied algorithm matches standard algorithm" );

Assert.AreEqual(
expectedOutput,
BitConverter.ToString(converted).Replace("-",""),
"Supplied algorithm matches expected output" );
}
}
}

现在,第一个断言通过,因为移植的代码返回与 Convert.FromBase64String 完全相同的结果,但第二个断言失败,因为这不是您所说的从 C++ 获得的输出。

据此我得出结论,最有可能造成差异的原因是调用 C++ 版本时,BackFrom64 的内容未正确初始化并且包含垃圾值 - 在对 OP 的编辑中提供的 BackFrom64 的值导致与计算结果相同的输出,因此预期结果会发生其他事情。编译 C++ 版本并使用给定的输入运行:

int main()
{
const char* input = "lWXtYpNpwBBXoLmcuktDqg==";

BYTE Input[100];
BYTE Output[100];

int length = strlen(input);

memcpy(Input, input, length);

int result = FromBase64(Output, Input, 100, length);

for (int i = 0; i < result; ++i)
std::cout << std::hex << (int)Output[i] << " ";

std::cout << "\n";
}

生成输出

95 65 ed 62 93 69 c0 10 57 a0 b9 9c ba 4b 43 aa

这再次表明它与标准算法相同。

关于c# - 将 C++ 方法转换为 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52442964/

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