【发布时间】:2009-03-29 21:29:48
【问题描述】:
这是我关于 C# 中的河豚问题的第三个线程。尽管事实上我无法在我的应用程序中实现河豚,但我决定将其用作外部 C++ dll。 请注意我已经尝试过 Blowfish.NET 和其他任何方法,问题是我正在将代码从 C++ 转换为 C#,并且 C# 代码必须与 C++ 代码完全相同。
到目前为止:
注意导出的函数在代码末尾
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#代码(函数调用)
-初始化河豚键
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);
}
}
在 keyarray2 被 Encode 函数覆盖后,我通过解密检查 C++ 代码中的值以确保一切正常。
好吧,这不太好。那是我的问题,这就是我向你寻求帮助的原因。
解密时值不同,但如果我在C++源代码中加密和解密,它们是相等的。C++代码是完全一样的,除了没有DLL,因为代码是C++。
可能是因为 Initialize 函数。几个月前我读到 C++ 中的数组是作为指针传递的。我不相信,但即便如此 - 这可能是问题吗?
我找不到线索。我用 C# 中的河豚浪费了我的生命。至少该解决方案应该有效,但它没有 - 为什么?
【问题讨论】:
-
你浪费了你的妻子? IMO有点过度反应..
标签: c# c++ function dll interop