【发布时间】:2013-01-16 00:00:32
【问题描述】:
所以我一直在努力解决这个问题。我正在尝试制作自己的 AES 128 库以与我的一个程序一起使用。该库经过测试并在 C++ 中工作(对于加密功能很好。我还没有实现其他功能)“加密”功能是这样的:
新代码
void Aes128Class::EncryptBlock(BYTE* outBlock, const BYTE* inBlock, const BYTE* cipherBlock)
{
BYTE temp[16] = {0x00};
Galois::XorBlock(temp, inBlock);
Galois::XorBlock(temp, cipherBlock);
BYTE expandedKey[176] = {0x00};
memcpy(expandedKey, Key, 16);
Galois::expand_key(expandedKey);
Galois::XorBlock(temp, expandedKey);
for(int i=16; i<160; i+=16)
{
Galois::DoRound(temp, &expandedKey[i]);
}
Galois::SubBytes(temp);
Galois::ShiftRows(temp);
Galois::XorBlock(temp, &expandedKey[160]);
memcpy(outBlock, temp, 16);
}
void Aes128Class::EncryptData(BYTE* outBlock, size_t& outlen, const BYTE* inBlock, size_t length)
{
float blockSize = (float)(length/16);
blockSize = ceilf(blockSize);
int newLength = (int)(blockSize*16);
BYTE* temp = (BYTE*)malloc(newLength);
BYTE* padd = (BYTE*)malloc(newLength);
memset(temp, 0, newLength);
memcpy(padd, inBlock, length);
EncryptBlock(temp, padd, IV);
for (int i=1; i<blockSize; i++)
{
EncryptBlock(&temp[i*16], &padd[i*16], &temp[(i-1)*16]);
}
outlen = newLength;
memcpy(outBlock, temp, newLength);
}
这个想法是,如果 plainText 不是 16 字节块增量,那么我强制它是。所以这形成了一个可变大小的字节数组。它在我的 C++ 测试中有效,但是当我在 C# 中调用它时,我得到了一些不同的错误......这需要一分钟来描述。
[DllImport("CppAes128.dll", CallingConvention = CallingConvention.ThisCall,
EntryPoint = "?EncryptData@Aes128Class@@QAEXPAEAAIPBEI@Z")]
static extern void EncryptData(IntPtr pClass, ref IntPtr outblock, [Out]int OutLength, byte[] inBlock, int length);
当我调用它时,我得到了指向array 和outlength 的有效指针。它现在看起来的方式会导致访问冲突,但如果我将[Out]int OutLength 更改为ref IntPtr,我可以让该结构工作。有趣的是,如果我这样做 ref int 或 ref uint 它仍然“有效”。因此,如果我这样做,我会尝试阅读intptr,然后我会遇到访问冲突。我将其编译为.NET 4.0 中的x86 project(因为我在某处读到3.5 有一些访问错误......)
这是我在C# 中尝试过的。我玩了几个小时有点乱码(抱歉):
public byte[] EncryptData(byte[] plainText, int length)
{
byte[] enc = null;
int len = 0;
IntPtr pArray = IntPtr.Zero;
EncryptData(theClass, ref pArray, len, plainText, length);
Console.WriteLine(len);
//enc = new byte[len];
//Marshal.Copy(pArray, enc, 0, len);
//Marshal.Release(pArray);
//try
//{
// int elementSize = Marshal.SizeOf(typeof(IntPtr));
// //IntPtr unmanagedArray = Marshal.AllocHGlobal(10 * elementSize);
// Console.WriteLine("Reading unmanaged memory:");
// // Print the 10 elements of the C-style unmanagedArray
// for (int i = 0; i < 10; i++)
// {
// Console.WriteLine("{0:X2}:", Marshal.ReadByte(pArray, i));
// }
// Marshal.FreeHGlobal(pArray);
//}
//catch (Exception ex)
//{
// Console.WriteLine("{0}\n{1}", ex.Source, ex.Message);
// Console.WriteLine("Win32({0})", Marshal.GetLastWin32Error());
//}
//Marshal.Release(pArray);
return enc;
}
唯一有效的时候是我刚刚制作了一个静态大小的数组并且没有使用 ref 或 marshal 复制或任何东西。我认为我的签名是这样的
static extern void EncryptData(IntPtr pClass, byte[] outBlock, byte[] inBlock, int length);
这几乎奏效了,但问题是当我在 array 上执行 foreach 循环时,它始终是我放置的大小.. 至少可以说令人沮丧。
那我做错了什么?我怎样才能让它工作?我对此感到非常沮丧。谢谢
哦,仅供参考,这样我就不能再依赖cryptlib了。我正在尝试重新编译一个不同的项目,该项目使用cryptlib 作为静态库而不是共享的,这会导致我的编译选项出现一些问题,而且改回来太麻烦了。
已编辑以显示更多代码
这是我使用的测试。我找到了一个显示一堆测试的网页,所以这是我实现的。
void VerifyEncrypt16(const BYTE* expected, const BYTE* key, const BYTE* iv, const BYTE* plainText)
{
BYTE actual[16] = {0x00};
Aes128Class aes;
aes.SetKey(key, 16);
aes.SetIV(iv, 16);
size_t len = 0;
aes.EncryptData(actual, len, plainText, 16);
_ASSERT(CompareTwoArrays(expected, actual));
}
void VerifyEncrypt16String(const char* expected, const char* key, const char* iv, const char* plainText)
{
BYTE e[16];
BYTE k[16];
BYTE i[16];
BYTE p[16];
ByteUtil::StringToHex(expected, e);
ByteUtil::StringToHex(key, k);
ByteUtil::StringToHex(iv, i);
ByteUtil::StringToHex(plainText, p);
VerifyEncrypt16(e, k, i, p);
}
void CheckEncrypt16(void)
{
_RPT0(_CRT_WARN, "Checking Encryption of a 16 byte number IV set to 0\n");
//AESVS GFSbox test data for CBC
VerifyEncrypt16String("0336763e966d92595a567cc9ce537f5e","00000000000000000000000000000000","00000000000000000000000000000000","f34481ec3cc627bacd5dc3fb08f273e6");
VerifyEncrypt16String("a9a1631bf4996954ebc093957b234589","00000000000000000000000000000000","00000000000000000000000000000000","9798c4640bad75c7c3227db910174e72");
VerifyEncrypt16String("ff4f8391a6a40ca5b25d23bedd44a597","00000000000000000000000000000000","00000000000000000000000000000000","96ab5c2ff612d9dfaae8c31f30c42168");
VerifyEncrypt16String("dc43be40be0e53712f7e2bf5ca707209","00000000000000000000000000000000","00000000000000000000000000000000","6a118a874519e64e9963798a503f1d35");
VerifyEncrypt16String("92beedab1895a94faa69b632e5cc47ce","00000000000000000000000000000000","00000000000000000000000000000000","cb9fceec81286ca3e989bd979b0cb284");
VerifyEncrypt16String("459264f4798f6a78bacb89c15ed3d601","00000000000000000000000000000000","00000000000000000000000000000000","b26aeb1874e47ca8358ff22378f09144");
VerifyEncrypt16String("08a4e2efec8a8e3312ca7460b9040bbf","00000000000000000000000000000000","00000000000000000000000000000000","58c8e00b2631686d54eab84b91f0aca1");
//AESVS KeySbox test data for CBC
VerifyEncrypt16String("6d251e6944b051e04eaa6fb4dbf78465","10a58869d74be5a374cf867cfb473859","00000000000000000000000000000000","00000000000000000000000000000000");
//A TON OF MORE TESTS! etc etc etc VerifyEncrypt16String("5c005e72c1418c44f569f2ea33ba54f3","00000000000000000000000000000000","00000000000000000000000000000000","fffffffffffffffffffffffffffffffe");
VerifyEncrypt16String("3f5b8cc9ea855a0afa7347d23e8d664e","00000000000000000000000000000000","00000000000000000000000000000000","ffffffffffffffffffffffffffffffff");
}
【问题讨论】:
-
看看这个answer。简而言之,C# 无法知道你在 C++ 代码中分配了多少内存。
-
我不确定您的最终目标是什么,但我只想指出,C# 在 System.Security.Cryptography Namespace 中包含一些很棒的密码学类,效果很好。
-
与 C++ 代码互操作的必要起点是您开始使用可以从其他 C++ 代码安全调用的 C++ 代码。您还没有到那里,您正在严重泄漏内存,C++ 调用者也不会猜测所需的缓冲区大小。当您从 C# 进行调用时,这些问题不会得到改善。
-
@LeonNewswanger 我使用过这个命名空间并且喜欢它。我想我只是对需要使用 C# 进行测试有点娘娘腔,但是如果我想在另一个程序中使用这个驱动程序,我希望能够在 C# 中使用这个驱动程序。
-
@Alex 我还没有尝试过该帖子中建议的方法,我会试一试。我刚刚找到了一个文件(我认为来自 wiki),其中包含大约 300 个测试,并且它们都在 C++ 中传递。我在 C# 中禁用了对该驱动程序的调用,除了调用 RunAllTests 方法。
标签: c# c++ pointers marshalling