C# 密码生成器

因为公司经常换密码,需要一个密码生成器。
度娘后,直接开干!

Gitee地址:https://gitee.com/lucien2009/passwd-gen/tree/master

先上图:

20210730更新:
新增了几个功能

  1. 密码生成
  2. GUID生成
  3. 拼音首字母转换

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
文件下载:链接: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;
        }        
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用C#内置的`Random`类来生成随机密码,然后使用密码强度校验器来确保密码强度。以下是一个示例代码: ```csharp using System; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Web.Security; public static class PasswordGenerator { private static readonly Random Random = new Random(); public static string GeneratePassword(int length = 12) { const string validChars = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*?_-"; var password = new char[length]; for (var i = 0; i < length; i++) { var index = Random.Next(validChars.Length); password[i] = validChars[index]; } var passwordString = new string(password); var passwordValidator = new PasswordStrengthChecker(); var validationResult = passwordValidator.CheckPasswordStrength(passwordString); if (validationResult > MembershipPasswordStrength.Strong) { return passwordString; } return GeneratePassword(length); } } public class PasswordStrengthChecker { public MembershipPasswordStrength CheckPasswordStrength(string password) { var score = 0; var hasDigit = false; var hasLowerCase = false; var hasUpperCase = false; var hasNonAlphanumeric = false; foreach (var c in password) { if (char.IsDigit(c)) { hasDigit = true; score += 1; } else if (char.IsLower(c)) { hasLowerCase = true; score += 1; } else if (char.IsUpper(c)) { hasUpperCase = true; score += 1; } else { hasNonAlphanumeric = true; score += 2; } } if (!hasDigit || !hasLowerCase || !hasUpperCase || !hasNonAlphanumeric) { score = Math.Max(1, score); } if (password.Length < 8) { score = Math.Max(1, score); } else if (password.Length < 11) { score += 1; } else { score += 2; } if (score <= 1) { return MembershipPasswordStrength.Weak; } if (score <= 3) { return MembershipPasswordStrength.Medium; } if (score <= 6) { return MembershipPasswordStrength.Strong; } return MembershipPasswordStrength.VeryStrong; } } ``` 这个代码使用`Random`类生成随机数,并使用自定义的`PasswordStrengthChecker`类校验密码强度。你可以按照自己的需要修改密码长度和密码强度校验规则。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

若行若冲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值