【问题标题】:How to get cryptographically secure randomness with c++/cx on Windows Phone?如何在 Windows Phone 上使用 c++/cx 获得加密安全随机性?
【发布时间】:2014-07-13 22:37:34
【问题描述】:

显然.NET 中有System.Security.Cryptography.RNGCryptoServiceProvider 类。但这是我的理解(我是 windows phone 8 开发的新手),我无法从 c++/cx 访问该功能......或者我可以吗?

我无法在 Windows Phone API 中找到我可能使用的任何其他函数/类。我错过了什么?

【问题讨论】:

  • 我知道 Windows Phone API 与全功能 Windows 相比“不同”,但 Crypto API 在该平台上是否也可用(在某种程度上)?即,除非您的 C++ 端代码是托管的(在这种情况下,您可能已经知道(或应该)如何在托管代码中使用 System.Security.Cryptography,基本加密 API ala CryptGenRandom 似乎很适合 C++ 端如果它可用。我像瘟疫一样避免使用 WinPhone,所以如果该平台上不可用,我提前道歉。
  • 遗憾的是,CryptGenRandom 在 WP8 上似乎不可用。

标签: c++ windows-phone-8 windows-phone-8.1 c++-cx


【解决方案1】:

我发现我实际上可以使用 c++/cx 中的 WinRT Windows.Security.Cryptography API。

解决办法是简单

auto iBuffer = Windows::Security::Cryptography::CryptographicBuffer::GenerateRandom(rand_len);

为了从 iBuffer 中获取数据,我使用了this answer

auto reader = Windows::Storage::Streams::DataReader::FromBuffer(iBuffer);
std::vector<unsigned char> data(reader->UnconsumedBufferLength);
if (!data.empty())
    reader->ReadBytes(
        ::Platform::ArrayReference<unsigned char>(
            &data[0], data.size()
        )
    );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-15
    相关资源
    最近更新 更多