>
通过程序自动生成密码本
2024-01-16 15:19
.NET Core
  • 1057
  • 527
  • 64
  • 51

通过程序来生成密码本--加强密码的难度

  1.  static void Main(string[] args)
  2.  {
  3.      // 定义生成密码的数量
  4.      int count = 100;
  5.      ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
  6.      // 生成随机密码集合
  7.      var passwords = GeneratePasswords(count);
  8.      // 导出到 Excel 文件 
  9.      ExportToExcel(passwords);
  10.  }
  11.  static string[] GeneratePasswords(int count)
  12.  {
  13.      // 定义密码长度和字符集
  14.      int length = 8;
  15.      string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+";
  16.      // 生成随机密码集合
  17.      var random = new Random();
  18.      var passwords = new string[count];
  19.      for (int i = 0; i < count; i++)
  20.      {
  21.          var password = new char[length];
  22.          for (int j = 0; j < length; j++)
  23.          {
  24.              password[j] = chars[random.Next(chars.Length)];
  25.          }
  26.          passwords[i] = new string(password);
  27.      }
  28.      return passwords;
  29.  }
  30.  static void ExportToExcel(string[] passwords)
  31.  {
  32.      // 创建 Excel 文件
  33.      var fileInfo = new FileInfo("passwords.xlsx");
  34.      using (var package = new ExcelPackage(fileInfo))
  35.      {
  36.          // 添加工作表
  37.          var worksheet = package.Workbook.Worksheets.Add("Passwords");
  38.          // 添加表头
  39.          worksheet.Cells[11].Value = "Password";
  40.          // 添加数据
  41.          for (int i = 0; i < passwords.Length; i++)
  42.          {
  43.              worksheet.Cells[i + 21].Value = passwords[i];
  44.          }
  45.          // 保存文件
  46.          package.Save();
  47.      }
  48.  }



全部留言 ()
返回
顶部