如何在Windows Phone上使用c++/cx获得加密安全随机性

How to get cryptographically secure randomness with c++/cx on Windows Phone?

本文关键字:cx 加密 随机性 安全 c++ Windows Phone      更新时间:2023-10-16

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

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

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

解决方法是简单地

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

从iBuffer中获取数据,我使用了这个答案:

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()
        )
    );