none
Anyone knows WinRT API for Cryptographically strong Pseudo random number?

    Question

  • Hi,

    What is counterpart of BCryptGenRandom()/CryptGenRandom() in WinRT C++?

    Thanks,


    • Edited by Mr_Jones_ Thursday, April 19, 2012 4:49 PM
    Thursday, April 19, 2012 4:33 AM

Answers

  • Please refer to CryptographicBuffer.GenerateRandom method.

    Here is sample code.

    using Windows.Security.Cryptography;
    
    public String GenerateRndData()
    {
    	// Define the length, in bytes, of the buffer.
    	UInt32 length = 32;
    
    	// Generate random data and copy it to a buffer.
    	IBuffer buffer = CryptographicBuffer.GenerateRandom(length);
    
    	// Encode the buffer to a hexadecimal string (for display).
    	String hexRnd = CryptographicBuffer.EncodeToHexString(buffer);
    
    	return hexRnd;
    }
    
    public UInt32 GenerateRndNumber()
    {
    	// Generate a random number.
    	UInt32 Rnd = CryptographicBuffer.GenerateRandomNumber();
    
    	return Rnd;
    }
    


    Best wishes,


    Robin [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, April 20, 2012 7:38 AM

All replies

  • hello, Jones:

    is this what you're looking for?

                UInt32 Rnd = CryptographicBuffer.GenerateRandomNumber();

    I'm not familiar with crypto, so just for your referrence.


    ---------------------------------------------- JohnYe from SHANGHAI. email: yechzh@126.com

    Friday, April 20, 2012 6:12 AM
  • Please refer to CryptographicBuffer.GenerateRandom method.

    Here is sample code.

    using Windows.Security.Cryptography;
    
    public String GenerateRndData()
    {
    	// Define the length, in bytes, of the buffer.
    	UInt32 length = 32;
    
    	// Generate random data and copy it to a buffer.
    	IBuffer buffer = CryptographicBuffer.GenerateRandom(length);
    
    	// Encode the buffer to a hexadecimal string (for display).
    	String hexRnd = CryptographicBuffer.EncodeToHexString(buffer);
    
    	return hexRnd;
    }
    
    public UInt32 GenerateRndNumber()
    {
    	// Generate a random number.
    	UInt32 Rnd = CryptographicBuffer.GenerateRandomNumber();
    
    	return Rnd;
    }
    


    Best wishes,


    Robin [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, April 20, 2012 7:38 AM
  • How to get actual bytes out of Ibuffer  ?
    Friday, May 25, 2012 12:40 AM
  • You can load it into a DataReader: DataReader.FromBuffer
    If you want the raw bytes you can query for its IBufferByteAccess interface

    --Rob

    Friday, May 25, 2012 2:01 AM