I need to generate cryptographically strong random alphanumeric strings with a specified length, only using the following characters.
Is there a way to accomplish this in C#?
You can use class RandomNumberGenerator
to generate cryptographically-secure random numbers to do this, for example:
string allowed = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
int strlen = 10; // Or whatever
char[] randomChars = new char[strlen];
for (int i = 0; i < strlen; i++)
{
randomChars[i] = allowed[RandomNumberGenerator.GetInt32(0, allowed.Length)];
}
string result = new string(randomChars);
Console.WriteLine(result);