1
$\begingroup$

Have a look at the following codesample written in C#:

private static byte[] GetRandomBytes(int size)
{
    byte[] buff = new byte[size];
    // Create an array of crypto-secure random numbers
    using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
    {
        rng.GetBytes(buff);
    }

    // Gets a random byte from a non-crypto-secure RNG and xors each element with it
    // Alternatively, xor each element with a new random number
    int unsafeRand = (byte)_random.Next(0, 255);
    for (int i = 0; i < size; i++)
        buff[i] ^= unsafeRand;

    return buff;
}

Is it safe to combine the .NET cryptographically secure random number generators with an imperfect random number generator, for example via XOR?

$\endgroup$
5
  • $\begingroup$ You do understand that the code you provided, wouldn't actually generate proper pseudo-random generated numbers, right? There are several reasons for that, far to many to list, even in an answer honestly. How you fix this code wouldn't be far to complicated for an answer here. $\endgroup$
    – Ramhound
    Commented Jul 3, 2016 at 21:10
  • 1
    $\begingroup$ in theory, yes, mixing different source of randomness, one of which could weak/compromised, can still be secure: a bad apple does NOT spoil the whole bunch, provided the data is mixed appropriately. $\endgroup$
    – dandavis
    Commented Jul 4, 2016 at 0:09
  • $\begingroup$ it would be better to concat two chunks and use SHA3 to generate new bytes, if you're not trying to stretch. $\endgroup$
    – dandavis
    Commented Jul 4, 2016 at 0:52
  • $\begingroup$ I have to ask what is the point? What are you trying to do? $\endgroup$
    – otus
    Commented Jul 4, 2016 at 6:56
  • $\begingroup$ An interesting reading about mixing random sources: blog.cr.yp.to/20140205-entropy.html (Daniel J. Berstein) $\endgroup$
    – ddddavidee
    Commented Jul 4, 2016 at 10:17

2 Answers 2

4
$\begingroup$

Yes, if you XOR any values with a secure random numbers, you end up with secure random numbers.

You can easily see this by assuming the insecure random number is a constant instead. It only changes what the numbers are, it does not affect their relative probabilities.

$\endgroup$
1
$\begingroup$

Yes you can for the simple reason that XOR'ing a completely random stream with any value will still result in a random stream - although obviously very dependent for the randomness on the source stream.

There is however absolutely no point in doing so. XOR-ing a stream with a well seeded PRNG could be a good idea, but XOR'ing it with a single byte value (random or not) or a non-cryptographically secure stream doesn't bring you any additional security. Basically you would be wasting CPU cycles for no good reason at all.

$\endgroup$
1
  • $\begingroup$ I was going to comment: if you have any additional entropy you can use it to reseed RNGCryptoServiceProvider but apparently the user isn't allowed to reseed instances of that class at all - which is weird (it's definitely not the only thing weird about the MS implementation). $\endgroup$
    – Maarten Bodewes
    Commented Jul 4, 2016 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.