问题描述
这是我的第三个线程关于一个blowfish问题在C#。尽管事实我不能在我的应用程序中实现blowfish,我决定使用它作为外部C ++ DLL。
请注意,我试过Blowfish.NET和任何其他,问题是我正在将代码从C ++转换为C#和C#代码必须做的完全相同的C ++代码。
This is my 3rd thread concerning a blowfish problem in C#.Despite the fact I cannot have blowfish implemented in my application, I decided to use it as an external C++ dll.Please note I've tried Blowfish.NET and any other, the problem is that I'm translating code from C++ to C# and the C# code must do exactly the same as the C++ code does.
到目前为止:
注意导出的函数在代码末尾
C#代码(定义)
[DllImport("TestDLL.dll", EntryPoint = "Initkey" ,ExactSpelling = true , CallingConvention = CallingConvention.Cdecl)]
public static unsafe extern void Initkey(byte[] key);
[DllImport("TestDLL.dll", EntryPoint = "encode", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static unsafe extern void encode(UInt32 *stream);
C#代码(函数调用)
C# code(function calling)
-Initialize blowfish key
-Initialize blowfish key
UInt32[] keyarray = new UInt32[2];
//some code
Extern.Initkey(Misc.ConvertFromUInt32Array(keyarray));
//
//
//Helper function used to convert a UInt32 array into Byte array.
public static byte[] ConvertFromUInt32Array(UInt32[] array)
{
List<byte> results = new List<byte>();
foreach (UInt32 value in array)
{
byte[] converted = BitConverter.GetBytes(value);
results.AddRange(converted);
}
return results.ToArray();
}
- 编码数据。
UInt32[] keyarray2 = new UInt32[2];
//some code
unsafe
{
fixed (UInt32* LPBYTE = keyarray2)
{
Extern.encode(LPBYTE);
}
}
在Encode函数覆盖keyarray2后,在C ++代码中的值通过解密它们以确保一切都是好的。
After keyarray2 is overwritten by the Encode function, I check the values in the C++ code by decrypting them to make sure everything is alright.
好吧,这不是好的。这是我的问题,这就是为什么我要求你的帮助。
Well, It's not alright.That's my problem, That's why I am asking you for your help.
当我解密它们时,值是不同的,但是如果我在C ++源代码中加密和解密它们,它们是相等的.C ++代码是完全相同的,除了没有DLL,因为代码是在C ++。
The values are different when I decrypt them,but If I encrypt them and decrypt them in the C++ source, they are equal.The C++ code is absolutely the same,except that there's no DLL since the code is in C++.
这可能是因为Initialize函数。我读了几个月前,C ++中的数组作为指针传递。我不相信,但即使如此 - 可能是这个问题吗?
Could that be, because of the Initialize function.I had read a couple of months ago that arrays in C++ are passed as Pointers.I don't believe it,but even so - could that be the problem?
我找不到一个线索。我已经浪费了我的生活与C#中的河豚。至少这个解决方案应该工作,但它不 - 为什么?
I can't find a clue. I have wasted my life with that blowfish in C#. At least that solution should work, but it doesn't - Why?
推荐答案
要添加到什么jachymko说, BitConverter的文档 - 你需要确保你按照你想要的字节顺序传递密钥和数据。
注意 - 从您的上一个线程,我成功地使用修改的Blowfish.NET加密器加密数据,并让它匹配您的C ++代码的结果。
To add to what jachymko has said, also check the documentation for BitConverter - you need to be sure you are passing the key and data in the byte order you intended.Note - from your previous thread, I successfully encrypted data using the modified Blowfish.NET encryptor and got it to match the result of your C++ code.
这篇关于调用C ++ dll函数从C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!