create a password with uppercase letters in csharp

To create a password with uppercase letters in C#, you can use the following code:

main.cs
using System;
using System.Security.Cryptography;
using System.Text;

public static string GeneratePassword(int length)
{
    const string allowedChars = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
    var randNum = new byte[1];
    var crypto = new RNGCryptoServiceProvider();
    var stringBuilder = new StringBuilder();
    for (var i = 0; i < length; i++)
    {
        do
        {
            crypto.GetBytes(randNum);
        } while (randNum[0] >= allowedChars.Length * (byte.MaxValue / allowedChars.Length));
        stringBuilder.Append(allowedChars[randNum[0] % allowedChars.Length]);
    }
    return stringBuilder.ToString();
}
626 chars
21 lines

This function generates a random password with uppercase letters only. The length parameter specifies the length of the password that you want to generate. The const string allowedChars = "ABCDEFGHJKLMNOPQRSTUVWXYZ"; line sets the characters that are allowed in the password.

gistlibby LogSnag