I must be having some sort of google-block today, because I can't find an answer to what should be a common issue. I'm coding my password reset section on my site and I want to e-mail the user a URL containing a suffix of 128 characters of entropy that will be used to confirm the generation of a new password. I've looked around and using the Random function isn't recommended as it's not random enough, so I'm looking at using the RNGCryptoServiceProvider to generate the key. However I don't seem to be able to use this to generate a suitable 128 char string.
I've started off trying
byte[] linkBytes = new byte[128];
System.Security.Cryptography.RNGCryptoServiceProvider rngCrypto = new System.Security.Cryptography.RNGCryptoServiceProvider();
rngCrypto.GetBytes(linkBytes);
string text128 = Convert.ToBase64String(linkBytes);
string text128Enc = Uri.EscapeDataString(text128);
I can use the Uri escaping to encode any URL unfreindly characters and then decode them at the destination, but this generates 172 characters not 128 and I have to be a bit careful (as far as I am aware) or the string lenght due to limits on the URL length in IIS.
What am I doing wrong or is there another way I can generate 128 characters using RNGCrypto? I've seen some code that uses the modulus of 62 of each byte result on a string containing a-zA-Z0-9 (like this)
.....
foreach (byte b in data)
{
result.Append(chars[b%(chars.Length - 1)]);
}
.....
but obviously unless the limit of the range of the numbers in the bytes was a multiple of 62, this would then skew the distribution of characters, so that method doesn't seem ideal.
Thanks MH