因为公司经常换密码,需要一个密码生成器。
度娘后,直接开干!
Gitee地址:https://gitee.com/lucien2009/passwd-gen/tree/master
先上图:
20210730更新:
新增了几个功能
- 密码生成
- GUID生成
- 拼音首字母转换
文件下载:链接:https://pan.baidu.com/s/18GvQtXa2V28mwW89lYrNPA
提取码:0wgt
新的下载地址:
https://download.csdn.net/download/coolhe21cn/20656362
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PasswdGen
{
public partial class Form1 : Form
{
//随机数
string rand = "";
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 验证输入生成数量
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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>
/// <param name="sender"></param>
/// <param name="e"></param>
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 (checkBox4.Checked)
{
rand += "_";
}
if (checkBox4.Checked)
{
for (int i = 65; i <= 90; i++)
{
rand += ((Char)i).ToString();
}
}
btnBuild.Enabled = false;
backgroundWorker1.RunWorkerAsync();
}
/// <summary>
/// 另存为文本文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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();
}
}
private void Form1_Load(object sender, EventArgs e)
{
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
//comboBox1.SelectedIndex = 4;
}
/// <summary>
/// 新线程生成密码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
Random r = new Random();
string num = tbBuildNum.Text.Trim();
//string num1 = comboBox1.Text.Trim();
string passwdLength = txtPasswdLength.Text.Trim(); // 密码长度
for (int i = 0; i < Convert.ToInt32(num); i++)
{
string pwd = "";
for (int l = 0; l < Convert.ToInt32(passwdLength); l++)
{
pwd += rand[r.Next(0, rand.Length)];
}
lstPwdList.Items.Add(pwd);
}
btnBuild.Enabled = true;
btnSaveAs.Enabled = true;
//progressBar1.Value = 100;
}
/// <summary>
/// 复制选中项
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDelete_Click(object sender, EventArgs e)
{
lstPwdList.Items.Remove(lstPwdList.SelectedItem);
}
/// <summary>
/// 生成密码选中项更改
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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>
/// <param name="sender"></param>
/// <param name="e"></param>
private void lstPwdList_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (lstPwdList.SelectedItems.Count > 0)
btnCopySelect_Click(sender, new EventArgs());
}
/// <summary>
/// 是否包含大写字母
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
if (!checkBox2.Checked && !checkBox1.Checked && !checkBox4.Checked)
{
checkBox4.Checked = true;
}
}
/// <summary>
/// 是否包含小写字母
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (!checkBox2.Checked && !checkBox1.Checked && !checkBox4.Checked)
{
checkBox2.Checked = true;
}
}
/// <summary>
/// 是否包含数字
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (!checkBox2.Checked && !checkBox1.Checked && !checkBox4.Checked)
{
checkBox1.Checked = true;
}
}
private void backgroundWorker1_DoWork_1(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(this.txtPasswdLength.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;
}
}
}