问题
a C++ blowfish supports encrypting a block with less than < 8 letters,while .NET doesn't.Why?
-->C# NET Blowfish<-- -->C++ Blowfish<--
In both C++ and C# applications,I encrypt the following array
byte response[6] =
{
0x00, 0x80, 0x01, 0x61, 0x05, 0x06
};
In both C++ and C# applications,I call the Encrypt function with same parameters.
C++
Blowfish.Encrypt((LPBYTE)responce + 2,(LPBYTE)responce + 2, 4);
C#
Blowfish.Encrypt(responce, 2, responce, 2, responce.Length - 2);
However,In C++ I get the data encrypted,but not in C#.
Line 45,47 and 49 in C# NET Blowfish are the problematic lines,after the calculation at line 45 - the result becomes 0. line 47 becomes 2 + 0 = 2 and line 49 2 is not less than 2,so there's no loop.
There's some padding in C++ blowfish,but I get lost when I try to understand it.
My problem is that in C#, the padding must be exactly the same as in C++,so I get a valid answer.I can't use random padding and then remove the useless bytes after decryption,because the server I send the encrypted array is not mine.
Could you point me to what padding is used in the C++ blowfish and how it should be implemented in C# NET.
//Note I need not send this packet multiple times,only one time.
Thanks in advance!
回答1:
The code you pastebinned (not recommended for SO by the way, as your paste will be removed if noone reads it in a while, thereby leaving this question broken) says in its header comment:
/// Note that the number of bytes must be adjusted to the block size of the algorithm.
(lines 3 and 4). So it seems to be behaving to spec, if it ignores your undersized data.
The C++ code seems to be padding with null bytes, the comment says:
// pad end of data with null bytes to complete encryption
It would be helpful to see the C++ function GetOutputLength()
, to see how it handles undersized inputs. I guess it rounds upwards, but it would be nice to verify.
来源:https://stackoverflow.com/questions/704402/c-blowfish-works-with-fewer-letters-but-c-sharp-blowfish-doesnt