0

So apparently there is the System.Security.Cryptography.RNGCryptoServiceProvider class available in .NET. But it's my understanding (I'm new to windows phone 8 development) that I can't access that function from c++/cx... or can I?

I've not been able to find any other function/class in the Windows Phone API that I might use. What am I missing?

A. Nilsson
  • 539
  • 3
  • 5
  • 19
  • I know the Windows Phone API is "different" compared to full-goose Windows, but isn't the Crypto API available (to some degree) on that platform as well? I.e.,unless your C++-side code is managed (in which case you likely already know (or should) how to use `System.Security.Cryptography` in managed code already, the base crypto api ala [`CryptGenRandom`](http://msdn.microsoft.com/en-us/library/windows/desktop/aa379942(v=vs.85).aspx) would seem a natural fit on the C++ side if it is available. I avoid WinPhone like the plague, so if this isn't available on that platform I apologize in advance. – WhozCraig May 24 '14 at 18:41
  • Sadly `CryptGenRandom` does not appear to be available on WP8. – A. Nilsson May 25 '14 at 07:22

1 Answers1

1

I found that I could actually use the WinRT Windows.Security.Cryptography API from c++/cx.

The solution was to simply

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

To get the data from the iBuffer I used 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()
        )
    );
Community
  • 1
  • 1
A. Nilsson
  • 539
  • 3
  • 5
  • 19