我正在将 C++ TCP 客户端转换为 C#。客户端用于使用河豚对数组的 4 个字节进行编码。
C++
BYTE response[6] =
{
0x00, 0x80, 0x01, 0x61, 0xF8, 0x17
};
// Encrypt the last 4 bytes of the packet only(0x01,0x061,0xF8,0x17)
blowfish.Encode(responce + 2, responce + 2, 4);
// Send the packet
send(s, (char*)sendPtr, sendSize, 0);
C#
responce = new byte[6] { 0x00, 0x80, 0x01, 0x61, 0xF8, 0x17};
// Encrypt the last 4 bytes of the packet only(0x01,0x061,0xF8,0x17)
Handshake.blowfish.Encrypt(responce, 2, responce, 2, 4);
// Send the packet
WS.sock.Send(encrypted);
在 C++ 代码中,当使用这些参数调用“Blowfish.Encode”行时,它会进入 cBlowfish.Encrypt 函数
DWORD cBlowFish::Encode(BYTE * pInput, BYTE * pOutput, DWORD lSize)
{
DWORD lCount, lOutSize, lGoodBytes;
BYTE *pi, *po;
int i, j;
int SameDest =(pInput == pOutput ? 1 : 0);
lOutSize = GetOutputLength(lSize);
for(lCount = 0; lCount < lOutSize; lCount += 8)
{
if(SameDest) // if encoded data is being written into input buffer
{
if(lCount < lSize - 7) // if not dealing with uneven bytes at end
{
Blowfish_encipher((DWORD *) pInput, (DWORD *)(pInput + 4));
}
else // pad end of data with null bytes to complete encryption
{
po = pInput + lSize; // point at byte past the end of actual data
j =(int)(lOutSize - lSize); // number of bytes to set to null
for(i = 0; i < j; i++)
*po++ = 0;
Blowfish_encipher((DWORD *) pInput, (DWORD *)(pInput + 4));
}
pInput += 8;
}
else // output buffer not equal to input buffer, so must copy
{ // input to output buffer prior to encrypting
if(lCount < lSize - 7) // if not dealing with uneven bytes at end
{
pi = pInput;
po = pOutput;
for(i = 0; i < 8; i++)
// copy bytes to output
*po++ = *pi++;
// now encrypt them
Blowfish_encipher((DWORD *) pOutput, (DWORD *)(pOutput + 4));
}
else // pad end of data with null bytes to complete encryption
{
lGoodBytes = lSize - lCount; // number of remaining data bytes
po = pOutput;
for(i = 0; i <(int) lGoodBytes; i++)
*po++ = *pInput++;
for(j = i; j < 8; j++)
*po++ = 0;
Blowfish_encipher((DWORD *) pOutput, (DWORD *)(pOutput + 4));
}
pInput += 8;
pOutput += 8;
}
}
return lOutSize;
}
为了清楚起见,由于传递的字节长度很短(4),循环只执行了一次。
从这个巨大的代码中只执行一个调用(只执行一次),调用是:
Blowfish_encipher((DWORD *) pInput, (DWORD *)(pInput + 4));
//意思是代码传递前两个if语句,然后离开循环和函数。
从我的角度来看,解决方案隐藏在加密函数内部的某个地方:
void cBlowFish::Blowfish_encipher(DWORD *xl, DWORD *xr)
{
union aword Xl, Xr;
Xl.dword = *xl;
Xr.dword = *xr;
Xl.dword ^= PArray [0];
ROUND(Xr, Xl, 1);
ROUND(Xl, Xr, 2);
ROUND(Xr, Xl, 3);
ROUND(Xl, Xr, 4);
ROUND(Xr, Xl, 5);
ROUND(Xl, Xr, 6);
ROUND(Xr, Xl, 7);
ROUND(Xl, Xr, 8);
ROUND(Xr, Xl, 9);
ROUND(Xl, Xr, 10);
ROUND(Xr, Xl, 11);
ROUND(Xl, Xr, 12);
ROUND(Xr, Xl, 13);
ROUND(Xl, Xr, 14);
ROUND(Xr, Xl, 15);
ROUND(Xl, Xr, 16);
Xr.dword ^= PArray [17];
*xr = Xl.dword;
*xl = Xr.dword;
}
定义:
#define S(x,i) (SBoxes[i][x.w.byte##i])
#define bf_F(x) (((S(x,0) + S(x,1)) ^ S(x,2)) + S(x,3))
#define ROUND(a,b,n) (a.dword ^= bf_F(b) ^ PArray[n])
问题是 C++ 中的 Blowfish_Encipher 函数有两个参数:Input(xl) as dword 和 Output(xr) as dword。
C# Blowfish Encrypt_Block 函数有四个参数,为什么?
public void EncryptBlock(uint hi,uint lo,out uint outHi,out uint outLo)
与 C++ 河豚不同,EncryptBlock 调用 Encrypt 而不是 Encrypt 来调用 EncryptBlock。也许 EncryptBlock 不是 C++ Blowfish_Encipher?
无论如何,我的问题是,当我用 6 个字节的数组调用 C++ 代码时,请求河豚只编码最后 4 个字节,它做到了。
如果我用这 4 个字节在 C# 中调用 encrypt 函数,它返回 0x00。(如果您想查看 C# Blowfish,请查看我的第一行 - 我已在此处添加了一个超链接)。
注意我不能改变数据包的结构,它应该是这样的,但是加密的。
我也试过这个:
知道C++ Encrpypt函数只执行一个调用-blowfish Encipher。我尝试在C#中直接调用EncryptBlock,但是输入和输出有Hi Uint32和Low Uint32,如何将它们传播为HI或LO?如果 Encrypt_Block 在 C# 中调用河豚加密,这会起作用吗?我不太确定。
先感谢您!