StringBuilder sb = new StringBuilder(); for( int i = 0; i < length; i++ ) { int num1 = Number(); Int32 ASCII = num1; num = (char)num1; sb.Append( num ); } Console.WriteLine( sb.ToString() );
This is not how I will create a password and how I will generate random text, but it will give you a line and answer the original question.
As for how I would accomplish this task:
System.Security.Cryptography.RNGCryptoServiceProvider _crypto = new System.Security.Cryptography.RNGCryptoServiceProvider(); byte[] bytes = new byte[8];
For completeness, the Base62 algorithm that I use is used here. Base62 has an advantage over the more commonly used Base64 in that it does not contain any special characters, so it is easy to use in query strings, HTML and JavaScript (with a few minor caveats). Of course, passwords should not be used in any of these places, and you may want to include special characters to make the password more complex.
No matter how I convert random numbers to Base62.
private static readonly char[] _base62Characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray(); public static string ToBase62String( long value ) { if( value < 0L ) { throw new ArgumentException( "Number must be zero or greater." ); } if( value == 0 ) { return "0"; } string retVal = ""; while( value > 0 ) { retVal = _base62Characters[value % 62] + retVal; value = value / 62; } return retVal; }
Finally, I want to point out that passwords are very rarely created for any purpose, because this means that they are distributed in one form or another. Passwords must be hashed and salted; password reset should be based on random, expired security tokens, allowing the user to reset at once. Passwords should never be emailed to the user; passwords should never be stored in clear text or in any reversible format.
To generate a password token, the reset code that I provided may work well, because it creates a large cryptographically random number encoded in an Internet-safe format. But even a hashed GUID could do the trick in this case.
source share