You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#简易密码生成器开发疑问:如何生成并输出用户定制密码

C#简易密码生成器实现方案

一、使用指定的四个For循环实现

你的现有代码已经完成了用户输入的获取,接下来可以通过四个循环分别从对应字符集中随机选取指定数量的字符,最后打乱字符顺序(避免密码呈现固定的大写→小写→数字→符号结构),再输出最终密码。

完整代码如下:

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

Console.WriteLine("Welcome 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());

// 存储生成的所有密码字符
List<char> passwordChars = new List<char>();

// 生成指定数量的大写字母
for (int i = 0; i < upperAmount; i++)
{
    int randomIndex = rnd.Next(upperCase.Length);
    passwordChars.Add(upperCase[randomIndex]);
}

// 生成指定数量的小写字母
for (int i = 0; i < lowerAmount; i++)
{
    int randomIndex = rnd.Next(lowerCase.Length);
    passwordChars.Add(lowerCase[randomIndex]);
}

// 生成指定数量的数字
for (int i = 0; i < numAmount; i++)
{
    int randomIndex = rnd.Next(numbers.Length);
    passwordChars.Add(numbers[randomIndex]);
}

// 生成指定数量的特殊字符
for (int i = 0; i < charAmount; i++)
{
    int randomIndex = rnd.Next(specChac.Length);
    passwordChars.Add(specChac[randomIndex]);
}

// 用Fisher-Yates洗牌算法打乱字符顺序
int charCount = passwordChars.Count;
while (charCount > 1)
{
    charCount--;
    int shuffleIndex = rnd.Next(charCount + 1);
    char temp = passwordChars[shuffleIndex];
    passwordChars[shuffleIndex] = passwordChars[charCount];
    passwordChars[charCount] = temp;
}

// 转换为字符串并输出
string finalPassword = new string(passwordChars.ToArray());
Console.WriteLine($"Generated Password: {finalPassword}");

二、其他实现思路

1. 随机选择字符类型生成(天然乱序)

先记录每种字符类型的剩余生成数量,每次循环随机挑选一种还有剩余数量的类型生成字符,无需额外洗牌:

// 省略前面的输入代码...

List<char> passwordChars = new List<char>();
int remainingUpper = upperAmount;
int remainingLower = lowerAmount;
int remainingNum = numAmount;
int remainingSpec = charAmount;
int totalLength = upperAmount + lowerAmount + numAmount + charAmount;

for (int i = 0; i < totalLength; i++)
{
    // 收集当前还能生成的字符类型
    List<int> availableTypes = new List<int>();
    if (remainingUpper > 0) availableTypes.Add(0);
    if (remainingLower > 0) availableTypes.Add(1);
    if (remainingNum > 0) availableTypes.Add(2);
    if (remainingSpec > 0) availableTypes.Add(3);

    // 随机选一种类型生成字符
    int selectedType = availableTypes[rnd.Next(availableTypes.Count)];
    switch (selectedType)
    {
        case 0:
            passwordChars.Add(upperCase[rnd.Next(upperCase.Length)]);
            remainingUpper--;
            break;
        case 1:
            passwordChars.Add(lowerCase[rnd.Next(lowerCase.Length)]);
            remainingLower--;
            break;
        case 2:
            passwordChars.Add(numbers[rnd.Next(numbers.Length)]);
            remainingNum--;
            break;
        case 3:
            passwordChars.Add(specChac[rnd.Next(specChac.Length)]);
            remainingSpec--;
            break;
    }
}

string finalPassword = new string(passwordChars.ToArray());
Console.WriteLine($"Generated Password: {finalPassword}");

2. 使用LINQ简化代码

借助LINQ的链式调用,可以用更简洁的语法完成字符生成与打乱:

// 省略前面的输入代码...

// 生成各类指定数量的随机字符
var upperChars = Enumerable.Repeat(upperCase, upperAmount).Select(s => s[rnd.Next(s.Length)]);
var lowerChars = Enumerable.Repeat(lowerCase, lowerAmount).Select(s => s[rnd.Next(s.Length)]);
var numChars = Enumerable.Repeat(numbers, numAmount).Select(s => s[rnd.Next(s.Length)]);
var specChars = Enumerable.Repeat(specChac, charAmount).Select(s => s[rnd.Next(s.Length)]);

// 合并所有字符并打乱顺序
var allChars = upperChars.Concat(lowerChars).Concat(numChars).Concat(specChars).OrderBy(_ => rnd.Next());
string finalPassword = new string(allChars.ToArray());

Console.WriteLine($"Generated Password: {finalPassword}");

内容的提问来源于stack exchange,提问作者Clinton Parris

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 18:10:50