Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RNGCryptoServiceProvider and Zeros?

walking through some cryptogtaphy stuff , I saw that RNGCryptoServiceProvider has 2 methods :

link

RNGCryptoServiceProvider.GetNonZeroBytes

and

RNGCryptoServiceProvider.GetBytes 

And so I ask :

What is odd with Filling an array of bytes with a cryptographically strong sequence of random value which some (0 or more) of them are zeros ? (it is random values and apparently there wont be many zeros , and still zero is also a regular number)

why did they created the distinguishing ?

like image 862
Royi Namir Avatar asked Oct 03 '12 07:10

Royi Namir


People also ask

What is RNGCryptoServiceProvider?

The RNGCryptoServiceProvider is used to populate a random byte array using the GetBytes method that is then printed out as a string in the following example: public static void BetterRandomString( ) { // create a stronger hash code using RNGCryptoServiceProvider byte[] random = new byte[64]; RNGCryptoServiceProvider ...

Is RNGCryptoServiceProvider obsolete?

RNGCryptoServiceProvider is marked as obsolete, starting in . NET 6.


1 Answers

Within the .NET framework, GetNonZeroBytes(byte[]) is used when generating PKCS#1 padding for RSA encryption, which uses 0x00 as a seperator.

Using a tool like Reflector, you can see it used in RSAPKCS1KeyExchangeFormatter.CreateKeyExchange(byte[]) to implement padding as per RFC 2313, section 8.1.2 (RFC 3218 has some nice ASCII art that demonstrates the byte layout more clearly).

GetNonZeroBytes(byte[]) could also be used to generate salt. The Cryptography StackExchange site has a similar question which suggests that avoiding 0x00 is to help with libraries and APIs that may treat the salt as a zero-terminated string, which would accidentally truncate the salt. However, unless one is using P/Invoke, this is unlikely to be a concern in .NET.

like image 123
Ashley Ross Avatar answered Oct 22 '22 11:10

Ashley Ross