0

最小限のパスワードを生成するパスワードジェネレーターを作成する必要があります。小文字 2 文字以上 大文字 2 文字以上 1桁以上 1 つの特殊文字。また、ユーザーが指定した長さである必要があります。このコードを使用してパスワードに最小限の長さを与えるにはどうすればよいですか? 例: 10?

これは私が苦しんでいるものですが、長さを制限しようとしましたが成功しなかったので、これらのコードを削除しました:

class Program
{

    private const string Capital = "ABCDEFGHIJKLMNOUP";
    private const string Lower = "abcdefghijklmnoup";
    private const string Digit = "1234567890";
    private const string Spec = "@&#><-[]Łł$ß";
    private const string AllChars = Capital + Lower + Digit + Spec;
    private static Random r = new Random();


    static void Main(string[] args)
    {

        StringBuilder password = new StringBuilder();

        for (int i = 1; i <= 2; i++)
        {
            char capitalLetter = GenerateChar(Capital);
            InsertAtRandomPos(password, capitalLetter);
        }
        for (int i = 1; i <= 2; i++)
        {
            char lowerLetter = GenerateChar(Lower);
            InsertAtRandomPos(password, lowerLetter);
        }
        for (int i = 1; i <= 3; i++)
        {
            char digit = GenerateChar(Digit);
            InsertAtRandomPos(password, digit);
        }
        for (int i = 1; i <= 1; i++)
        {
            char specialLetter = GenerateChar(Spec);
            InsertAtRandomPos(password, specialLetter);
        }
        Console.WriteLine("A jelszava: {0}",password);


        Console.ReadKey();
    }
    private static void InsertAtRandomPos(StringBuilder password, char character)
    {
        int randomPosition = r.Next(password.Length + 1);
        password.Insert(randomPosition, character);
    }
    private static char GenerateChar(string availableChars)
    {
        int randomIndex = r.Next(availableChars.Length);
        char randomChar = availableChars[randomIndex];
        return randomChar;
    }
}

}

4

1 に答える 1