0

I can't seem to figure out how to print out what the user has entered for their password once their finished answering the questions.

I want to use 4 different for loops for upperCase, lowerCase, numbers, symbols,based on what the user has entered. If anyone has any different ideas please share. It would be a great help. I'm new to programming.

Here is what I have so far

string upperCase = ("ABCDEFGHIJKLMNOPQRSTUVWXYZ") ;
string lowerCase = ("abcdefghijklmnopqrstuvwxyz");
string numbers = ("1234567890" );
string specChac = ("!@#$%^&*():<>?/");
string randomPassword = upperCase + lowerCase + numbers + specChac;
string allPasswords = "randomPassword";
Random rnd = new Random();

Console.WriteLine("Welocme to the C# password generator! ");
Console.WriteLine("----------------------------------------");

Console.WriteLine("How many uppercase letters would you like in your password ?");
int upperAmount = int.Parse(Console.ReadLine());
            
Console.WriteLine("How many lowercase letters would you like in your password ?");
int lowerAmount = int.Parse(Console.ReadLine());

Console.WriteLine("How many numbers would you like in your password ?");
int numAmount = int.Parse(Console.ReadLine());

Console.WriteLine("How many special characters would you like in your password?");
int charAmount = int.Parse(Console.ReadLine());
Stefan Wuebbe
  • 2,109
  • 5
  • 17
  • 28
  • Welcome to Stack Overflow. Please take the [tour] to learn how Stack Overflow works and read [ask] on how to improve the quality of your question. Please see: [Why is β€œCan someone help me?” not an actual question?](https://meta.stackoverflow.com/q/284236). It is unclear what you are asking or what the problem is. – Progman Dec 03 '22 at 23:10
  • Also good to read: https://stackoverflow.com/questions/14260107/how-secure-is-random-function-in-asp-net-c – Julian Dec 03 '22 at 23:28

1 Answers1

0

Well, there are many many ways on how to implement that. Here is one showing the concept of seeding.

Instead of defining the possible characters I just assign the possible minimum and maximum ASCII character code.
You can do it however you want.

internal static class RandomPswd
{
    const int CHAR_UPPER_MIN = 0x41;
    const int CHAR_UPPER_MAX = 0x5a;
    const int CHAR_LOWER_MIN = 0x61;
    const int CHAR_LOWER_MAX = 0x7a;
    const int CHAR_DIGIT_MIN = 0x30;
    const int CHAR_DIGIT_MAX = 0x39;
    const int CHAR_SYMBL_MIN = 0x21;
    const int CHAR_SYMBL_MAX = 0x2f;


    private static void Shuffle(char[] buffer)
    {
        int seed = GetSeed();
        Random r = new Random(seed);

        int n = buffer.Length;
        while (n > 1) {
            int k = r.Next(n--);
            char t = buffer[n];
            buffer[n] = buffer[k];
            buffer[k] = t;
        }
    }


    private static int GetSeed()
    {
        unchecked
        {
            int hash = (int)DateTime.Now.Ticks * 7302013 ^ Environment.UserName.GetHashCode();
            hash = hash * 7302013 ^ (CHAR_UPPER_MIN.GetHashCode());
            hash = hash * 7302013 ^ (CHAR_UPPER_MAX.GetHashCode());
            hash = hash * 7302013 ^ (CHAR_LOWER_MIN.GetHashCode());
            hash = hash * 7302013 ^ (CHAR_LOWER_MAX.GetHashCode());
            hash = hash * 7302013 ^ (CHAR_DIGIT_MIN.GetHashCode());
            hash = hash * 7302013 ^ (CHAR_DIGIT_MAX.GetHashCode());
            hash = hash * 7302013 ^ (CHAR_SYMBL_MIN.GetHashCode());
            hash = hash * 7302013 ^ (CHAR_SYMBL_MAX.GetHashCode());
            return hash;
        }               
    }

    public static string Create(int upper, int lower, int digits, int symbols)
    {
        char[] upperChars = new char[upper];
        char[] lowerChars = new char[lower];
        char[] digitChars = new char[digits];
        char[] symblChars = new char[symbols];

        int seed = GetSeed();
        Random r = new Random(seed);
        for (int i=0; i<upper; i++) {
            upperChars[i] = (char)r.Next(CHAR_UPPER_MIN, CHAR_UPPER_MAX);
        }

        seed = GetSeed();
        r = new Random(seed);
        for (int i = 0; i < lower; i++) {
            lowerChars[i] = (char)r.Next(CHAR_LOWER_MIN, CHAR_LOWER_MAX);
        }

        seed = GetSeed();
        r = new Random(seed);
        for (int i = 0; i < digits; i++) {
            digitChars[i] = (char)r.Next(CHAR_DIGIT_MIN, CHAR_DIGIT_MAX);
        }

        seed = GetSeed();
        r = new Random(seed);
        for (int i=0; i< symbols; i++) {
            symblChars[i] = (char)r.Next(CHAR_SYMBL_MIN, CHAR_SYMBL_MAX);
        }

        char[] buf = new char[upper + lower + digits + symbols];
        upperChars.CopyTo(buf, 0);
        lowerChars.CopyTo(buf, upper);
        digitChars.CopyTo(buf, upper + lower);
        symblChars.CopyTo(buf, upper + lower + digits);
        Shuffle(buf);

        return new string(buf);
    }
}
    
    
static void Main()
{
    var pw = RandomPswd.Create(12, 8, 7, 5);
}