什么加密随机生成器 C 代码可以在 WP8.1 上使用?
What crypto random generator C code could use on WP8.1?
我使用了一些 C 代码并将其编译到通用应用程序的 C++ WinRT 组件中。然后我根据手册编写了一个 C++ 包装器 class 以将 C 代码的功能公开给我的 C# Universal Store 项目:1, 2.
对于项目的 Windows 8.1 部分工作正常,但无法在 Windows Phone 8.1 上加载组件,只说 "The specified module could not be found"
。我发现问题是 C 代码使用了以下随机生成器函数:
# define RtlGenRandom SystemFunction036
# if defined(__cplusplus)
extern "C"
# endif
BOOLEAN NTAPI RtlGenRandom(PVOID RandomBuffer, ULONG RandomBufferLength);
# pragma comment(lib, "advapi32.lib")
在 WP8.1 上似乎不可用。
This MSDN page建议我们改用CryptGenRandom
,但后者驻留在Wincrypt.h
,WP8.1上没有。
那么,我们可以使用什么随机生成器来自 C 代码?
使用 WinRT Windows::Security::Cryptography [C++] API 。
CryptographicBuffer.GenerateRandom 会完成要求
我想要一些不涉及从 C 调用 C++/CX 代码的东西,但到目前为止一无所获。所以,下面是我现在使用的解决方案。
// C++/CX
using namespace Platform;
using namespace Windows::Security::Cryptography;
using namespace Windows::Storage::Streams;
extern "C" void GenerateRandomBytes(unsigned char *bytes, unsigned int length)
{
IBuffer^ buffer = CryptographicBuffer::GenerateRandom(length);
DataReader^ reader = DataReader::FromBuffer(buffer);
reader->ReadBytes(ArrayReference<unsigned char>(bytes, buffer->Length));
}
// C
#ifdef RtlGenRandom
RtlGenRandom((PVOID) buf, (ULONG) size);
#else
GenerateRandomBytes((unsigned char*)buf, (unsigned int)size);
#endif
我使用了一些 C 代码并将其编译到通用应用程序的 C++ WinRT 组件中。然后我根据手册编写了一个 C++ 包装器 class 以将 C 代码的功能公开给我的 C# Universal Store 项目:1, 2.
对于项目的 Windows 8.1 部分工作正常,但无法在 Windows Phone 8.1 上加载组件,只说 "The specified module could not be found"
。我发现问题是 C 代码使用了以下随机生成器函数:
# define RtlGenRandom SystemFunction036
# if defined(__cplusplus)
extern "C"
# endif
BOOLEAN NTAPI RtlGenRandom(PVOID RandomBuffer, ULONG RandomBufferLength);
# pragma comment(lib, "advapi32.lib")
在 WP8.1 上似乎不可用。
This MSDN page建议我们改用CryptGenRandom
,但后者驻留在Wincrypt.h
,WP8.1上没有。
那么,我们可以使用什么随机生成器来自 C 代码?
使用 WinRT Windows::Security::Cryptography [C++] API 。 CryptographicBuffer.GenerateRandom 会完成要求
我想要一些不涉及从 C 调用 C++/CX 代码的东西,但到目前为止一无所获。所以,下面是我现在使用的解决方案。
// C++/CX
using namespace Platform;
using namespace Windows::Security::Cryptography;
using namespace Windows::Storage::Streams;
extern "C" void GenerateRandomBytes(unsigned char *bytes, unsigned int length)
{
IBuffer^ buffer = CryptographicBuffer::GenerateRandom(length);
DataReader^ reader = DataReader::FromBuffer(buffer);
reader->ReadBytes(ArrayReference<unsigned char>(bytes, buffer->Length));
}
// C
#ifdef RtlGenRandom
RtlGenRandom((PVOID) buf, (ULONG) size);
#else
GenerateRandomBytes((unsigned char*)buf, (unsigned int)size);
#endif