1042 字
5 分钟
C#随机密码生成器
2025-12-21

注:保存的密码文件为明文保存如需加密可参考C# 文件加密解密工具#

跳转链接:C# 文件加密解密工具类

1. 简介#

这是一个用C#开发的随机密码生成器,可以根据用户需求生成不同强度和长度的随机密码,并将生成的密码保存到文件中。该程序简单实用,适合需要生成安全密码的场景。

2. 功能特点#

  • 支持4种不同强度的密码:
    1. 纯数字密码
    2. 数字+小写字母
    3. 数字+大小写字母
    4. 数字+大小写字母+特殊符号
  • 自定义密码长度
  • 自动记录生成时间
  • 密码自动保存到文件
  • 异常处理机制

3. 代码实现#

3.1 基础设置#

// 定义字符集
string[] uppercase = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "N", "M", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
string[] lowercase = new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "n", "m", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
string[] num = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
string[] symbols = new string[] { ".", "+", "-", "*", "/" };
// 初始化随机数生成器和文件写入器
Random rand = new Random();
StreamWriter streamWriter = new StreamWriter("password.txt", true);

3.2 主要逻辑#

Console.WriteLine("输入密码强度,输入1为纯数字,2为数字加小写字母,3为数字加上大小写字母,4为数字加上大小写字母加上符号");
while (true)
{
try
{
// 获取用户输入
int strength = int.Parse(Console.ReadLine());
Console.WriteLine("输入密码长度");
int length = int.Parse(Console.ReadLine());
// 根据强度生成密码
string password = GeneratePassword(strength, length);
// 显示并保存密码
Console.WriteLine(password);
SavePassword(password);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadKey();
}
}

3.3 密码生成方法#

private static string GeneratePassword(int strength, int length)
{
string password = "";
Random rand = new Random();
for (int i = 0; i < length; i++)
{
int choice = rand.Next(0, strength);
switch (strength)
{
case 1: // 纯数字
password += num[rand.Next(num.Length)];
break;
case 2: // 数字+小写字母
password += (choice == 0) ? num[rand.Next(num.Length)]
: lowercase[rand.Next(lowercase.Length)];
break;
case 3: // 数字+大小写字母
if (choice == 0) password += num[rand.Next(num.Length)];
else if (choice == 1) password += lowercase[rand.Next(lowercase.Length)];
else password += uppercase[rand.Next(uppercase.Length)];
break;
case 4: // 数字+大小写字母+符号
if (choice == 0) password += num[rand.Next(num.Length)];
else if (choice == 1) password += lowercase[rand.Next(lowercase.Length)];
else if (choice == 2) password += uppercase[rand.Next(uppercase.Length)];
else password += symbols[rand.Next(symbols.Length)];
break;
}
}
return password;
}

3.4 密码保存方法#

private static void SavePassword(string password)
{
using (StreamWriter streamWriter = new StreamWriter("password.txt", true))
{
streamWriter.WriteLine(DateTime.Now);
streamWriter.WriteLine(password);
streamWriter.WriteLine("---------------------------------");
streamWriter.Flush();
}
}

4. 使用方法#

  1. 运行程序
  2. 输入密码强度(1-4):
    • 1:纯数字
    • 2:数字+小写字母
    • 3:数字+大小写字母
    • 4:数字+大小写字母+特殊符号
  3. 输入密码长度
  4. 查看生成的密码
  5. 密码会自动保存到password.txt文件中

5. 完整代码#

using System;
using System.IO;
namespace 随机密码生成器
{
internal class Program
{
static void Main(string[] args)
{
string[] uppercase = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "N", "M", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
string[] lowercase = new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "n", "m", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
string[] num = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
string[] symbols = new string[] { ".", "+", "-", "*", "/" };
Random rand = new Random();
StreamWriter streamWriter = new StreamWriter("password.txt", true);
Console.WriteLine("输入密码强度,输入1为纯数字,2为数字加小写字母,3为数字加上大小写字母,4为数字加上大小写字母加上符号");
while (true)
{
string password = "";
try
{
int strength = int.Parse(Console.ReadLine());
Console.WriteLine("输入密码长度");
int length = int.Parse(Console.ReadLine());
password = GeneratePassword(strength, length, rand, uppercase, lowercase, num, symbols);
Console.WriteLine(password);
SavePassword(password, streamWriter);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadKey();
}
}
}
private static string GeneratePassword(int strength, int length, Random rand,
string[] uppercase, string[] lowercase, string[] num, string[] symbols)
{
string password = "";
for (int i = 0; i < length; i++)
{
int choice = rand.Next(0, strength);
switch (strength)
{
case 1:
password += num[rand.Next(num.Length)];
break;
case 2:
password += (choice == 0) ? num[rand.Next(num.Length)]
: lowercase[rand.Next(lowercase.Length)];
break;
case 3:
if (choice == 0) password += num[rand.Next(num.Length)];
else if (choice == 1) password += lowercase[rand.Next(lowercase.Length)];
else password += uppercase[rand.Next(uppercase.Length)];
break;
case 4:
if (choice == 0) password += num[rand.Next(num.Length)];
else if (choice == 1) password += lowercase[rand.Next(lowercase.Length)];
else if (choice == 2) password += uppercase[rand.Next(uppercase.Length)];
else password += symbols[rand.Next(symbols.Length)];
break;
}
}
return password;
}
private static void SavePassword(string password, StreamWriter streamWriter)
{
streamWriter.WriteLine(DateTime.Now);
streamWriter.WriteLine(password);
streamWriter.WriteLine("---------------------------------");
streamWriter.Flush();
}
}
}
C#随机密码生成器
https://non266.top/posts/ramdom-password/
作者
non_no266
发布于
2025-12-21
许可协议
CC BY-NC-SA 4.0

部分信息可能已经过时

封面
示例歌曲
示例艺术家
封面
示例歌曲
示例艺术家
0:00 / 0:00