C#:Blowfish 加密单个双字

标签 c# c++ encryption blowfish

我正在将 C++ TCP 客户端翻译成 C#。该客户端用于使用河豚对数组的 4 个字节进行编码。

C++ Blowfish

C# Blowfish(C# NET)

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++ blowfish 不同,EncryptBlock 调用 Encrypt 而不是 Encrypt 来调用 EncryptBlock。也许 EncryptBlock 不是 C++ Blowfish_Encipher?

无论如何,我的问题是,当我调用带有 6 字节数组的 C++ 代码时,要求河豚只编码最后 4 个字节,它做到了。

虽然如果我用这 4 个字节在 C# 中调用加密函数,它会返回 0x00。(如果您想查看 C# Blowfish,请查看我的第一行 - 我在那里添加了一个超链接)。

请注意,我无法更改数据包结构,它应该是那样,但已加密。

我也试过这个:

知道C++的Encrpypt函数只执行一个调用-blowfish Encipher。我试过在C#中直接调用EncryptBlock,但是输入和输出有Hi Uint32和Low Uint32,如何将它们分散成HI或LO?如果 Encrypt_Block 在 C# 中调用 blowfish Encrypt,这会起作用吗?我不太确定。

提前致谢!

最佳答案

Blowfish 在八字节 block 上工作。加密少于八字节(或八的倍数)的数据的唯一方法是将其填充(在本例中为零)。

您需要将一个八字节缓冲区传递到您的 C++ 函数中,因为您正在就地加密。 您发布的代码实际上将加密相邻内存的四个额外字节 ((DWORD *)(pInput + 4)),这显然不是您想要的。 此外,解密需要所有八个输出字节 - 因此,不幸的是,您不能只传递四个加密字节并期望它们在另一端成功解密。

我知道这不能解决您的问题 - 我看不到任何解决方法,因为您只想发送四个字节的加密数据,而 Blowfish 总是至少生成八个字节!

关于C#:Blowfish 加密单个双字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/705596/

相关文章:

C#如何上传大文件到ftp站点

docker - Asp.net 核心, "asn1 encoding routines:asn1_d2i_read_bio:not enough data"证书错误

java - 'ERR_OSSL_EVP_WRONG_FINAL_BLOCK_LENGTH' 在 Node js 中解密时

c# - 如何在 C# 中创建全局变量?

c# - ASP.NET MVC 4 脚本 bundle 在部署时导致错误

c++ - 没有goto跳入和跳出循环的更好方法?

c++ - OpenGL:wglGetProcAddress 总是返回 NULL

c++ 父类对象的集合

php安卓跨平台加密

c# - 这个简单的 Log4Net 包装器可以改进吗?