密码生成器,C#开发。基于.Net FrameWork 2.0
前段时间公司服务器出问题,密码被攻破导致管理员权限丢失。最终结果很悲惨,寄回服务器重装系统。为了加强安全性,决定定期更改服务器密码,于是写了这么一个小东西来生成随机密码。相信40位的大小写数字混合加下划线暴力破解的话,还是得费掉几台电脑才能破解出来吧,嘿嘿嘿嘿。。。。
程序很简单,实现了一些基本的功能:可选择生成密码位数、数量、大小写、是否包含数字与下划线,最多可一次生成9999个40位密码。采用多线程,加块密码生成速度,可将生成的密码保存为文本文件。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace PwdBuilder
{
public partial class Form1 : Form
{
/// <summary>
/// 窗体构造函数
/// </summary>
public Form1()
{
InitializeComponent();
}
//随机数
string rand = "";
/// <summary>
/// 窗体初始化
/// </summary>
private void Form1_Load(object sender, EventArgs e)
{
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
comboBox1.SelectedIndex = 4;
}
/// <summary>
/// 是否包含数字
/// </summary>
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (!checkBox2.Checked && !checkBox1.Checked && !checkBox4.Checked)
{
checkBox1.Checked = true;
}
}
/// <summary>
/// 是否包含小写字母
/// </summary>
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (!checkBox2.Checked && !checkBox1.Checked && !checkBox4.Checked)
{
checkBox2.Checked = true;
}
}
/// <summary>
/// 是否包含大写字母
/// </summary>
private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
if (!checkBox2.Checked && !checkBox1.Checked && !checkBox4.Checked)
{
checkBox4.Checked = true;
}
}
/// <summary>
/// 验证输入生成数量
/// </summary>
private void tbBuildNum_TextChanged(object sender, EventArgs e)
{
int i = 0;
if (!int.TryParse(tbBuildNum.Text.Trim(), out i))
{
MessageBox.Show("请输入大于0的任意数字!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
btnBuild.Enabled = false;
tbBuildNum.Focus();
tbBuildNum.SelectAll();
}
else if (i <= 0)
{
MessageBox.Show("请输入大于0的任意数字!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
btnBuild.Enabled = false;
tbBuildNum.Focus();
tbBuildNum.SelectAll();
}
else
btnBuild.Enabled = true;
}
/// <summary>
/// 开始生成
/// </summary>
private void btnBuild_Click(object sender, EventArgs e)
{
lstPwdList.Items.Clear();
rand = "";
if (checkBox1.Checked)
{
for (int i = 48; i <= 57; i++)
{
rand += ((Char)i).ToString();
}
}
if (checkBox2.Checked)
{
for (int i = 97; i <= 122; i++)
{
rand += ((Char)i).ToString();
}
}
if (checkBox3.Checked)
{
rand += "_";
}
if (checkBox4.Checked)
{
for (int i = 65; i <= 90; i++)
{
rand += ((Char)i).ToString();
}
}
btnBuild.Enabled = false;
backgroundWorker1.RunWorkerAsync();
}
/// <summary>
/// 生成密码选中项更改
/// </summary>
private void lstPwdList_SelectedIndexChanged(object sender, EventArgs e)
{
if (lstPwdList.SelectedItem != null)
{
btnDelete.Enabled = true;
btnCopySelect.Enabled = true;
}
else
{
btnDelete.Enabled = false;
btnCopySelect.Enabled = false;
}
}
/// <summary>
/// 删除选中项
/// </summary>
private void btnDelete_Click(object sender, EventArgs e)
{
lstPwdList.Items.Remove(lstPwdList.SelectedItem);
}
/// <summary>
/// 复制选中项
/// </summary>
private void btnCopySelect_Click(object sender, EventArgs e)
{
if (lstPwdList.SelectedItems.Count > 0)
{
Clipboard.SetText(lstPwdList.Text);
MessageBox.Show("已复制到系统剪贴板!请用Ctrl+V 或 鼠标右键粘贴.", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
/// <summary>
/// 另存为文本文件
/// </summary>
private void btnSaveAs_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string content = "";
for (int i = 0; i < lstPwdList.Items.Count; i++)
{
content += lstPwdList.GetItemText(lstPwdList.Items[i]) + "\r\n";
}
StreamWriter sw = new StreamWriter(saveFileDialog1.FileName);
sw.Write(content);
sw.Close();
}
}
/// <summary>
/// 鼠标双击复制
/// </summary>
private void lstPwdList_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (lstPwdList.SelectedItems.Count > 0)
btnCopySelect_Click(sender, new EventArgs());
}
/// <summary>
/// 新线程生成密码
/// </summary>
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
Random r = new Random();
for (int i = 0; i < Convert.ToInt32(tbBuildNum.Text.Trim()); i++)
{
string pwd = "";
for (int l = 0; l < Convert.ToInt32(comboBox1.Text.Trim()); l++)
{
pwd += rand[r.Next(0, rand.Length)];
}
lstPwdList.Items.Add(pwd);
if (i > 0)
{
backgroundWorker1.ReportProgress(Convert.ToInt32((i / Convert.ToDouble(tbBuildNum.Text.Trim())) * 100));
}
}
btnBuild.Enabled = true;
btnSaveAs.Enabled = true;
progressBar1.Value = 100;
}
/// <summary>
/// 线程生成绑定进度
/// </summary>
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
/// <summary>
/// 关于本程序
/// </summary>
private void btnInfo_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.ShowDialog(this);
}
}
}
该文章转载自网络大本营:http://xrss.cn/Dev/DotNet/20087320430.Html