编程语言
首页 > 编程语言> > C# 随机密码生成

C# 随机密码生成

作者:互联网

前言

效果浏览

​ 文末有Demo下载连接。

思路:

该小程序使用WPF写的,布局简单就不贴出来了

代码

        #region - 生成模式 -
        /*
        * 1、选择长度 
        *       密码长度
        * 2、选用模式
        *       使用什么组合? 默认 小写+数字 ,可选 大写 特殊字符。
        * 3、随机抽取范围
        *       给长度每位,使用 [随机范围] 填充 [每位长度]
        */

        //1、获取长度
        private int Get_PwdLeght()
        {
            if (cbb_lenght.SelectedIndex <= 4)
            {
                return Convert.ToInt32(cbb_lenght.SelectedItem.ToString());
            }
            else if (cbb_lenght.SelectedIndex == 5)
            {
                try
                {
                    return Int32.Parse(txt_selfDefine_lenght.Text);
                }
                catch (Exception)
                {
                    MessageBox.Show("请填阿拉伯数字");
                    return 0;
                }
            }
            else
            {
                return 0;
            }
        }

        //2、密码模式
        private PwdMode Get_PwdMode()
        {
            if (ckb_Up.IsChecked == false && ckb_Specific.IsChecked == false)
            {
                return PwdMode.Def;
            }
            else if (ckb_Up.IsChecked == true && ckb_Specific.IsChecked == false)
            {
                return PwdMode.Upper;
            }
            else if (ckb_Up.IsChecked == false && ckb_Specific.IsChecked == true)
            {
                return PwdMode.SpecialChar;
            }
            else
            {
                return PwdMode.UpperandSpecialChar;
            }
        }
        #endregion

        #region - 字符处理 -
        Random r = new Random();
        StringBuilder sb = new StringBuilder();
        byte[] buffer = new byte[1];

        //特殊字符
        List<string> specialChar = new List<string>() {
            "~",
            "!",
            "@",
            "#",
            "_",
            "$",
            "."
        };

        /// <summary>
        /// 获取 单个数字
        /// </summary>
        /// <returns></returns>
        private string Get_Num()
        {
            return r.Next(0, 9).ToString();
        }

        /// <summary>
        /// 获取 单个 小写字母
        /// </summary>
        /// <returns></returns>
        private string Get_LowerCase()
        {
            sb.Clear();
            for (int i = 97; i <= 122; i++)
            {
                buffer[0] = (byte)(i);
                sb.Append(Encoding.ASCII.GetString(buffer));
            }
            return sb[r.Next(0, sb.Length)].ToString();
        }

        /// <summary>
        /// 获取 单个 大写字母
        /// </summary>
        /// <returns></returns>
        private string Get_UpperCase()
        {
            sb.Clear();
            for (int i = 65; i <= 90; i++)
            {
                buffer[0] = (byte)(i);
                sb.Append(Encoding.ASCII.GetString(buffer));
            }
            return sb[r.Next(0, sb.Length)].ToString();
        }

        /// <summary>
        /// 获取 单个 特殊字符
        /// </summary>
        /// <returns></returns>
        private string Get_SpecialChar()
        {
            return specialChar[r.Next(0, specialChar.Count - 1)];
        }

        /// <summary>
        /// 字符处理 释放资源
        /// </summary>
        private void Character_Release()
        {
            if (sb != null)
            {
                sb.Clear();
                sb = null;
            }
            if (buffer != null)
                buffer = null;

            if (r != null)
                r = null;
        }
        #endregion
            
            StringBuilder sb2 = new StringBuilder();
private void btn_create_Click(object sender, RoutedEventArgs e)
{
    sb2.Clear();
    int temp =  0;
    for (int i = 0; i < Get_PwdLeght(); i++)
    {
        if (Get_PwdMode() == PwdMode.Def)
        {
            temp = r.Next(0, 2);
            if (temp == 0)
                sb2.Append(Get_Num());
            else
                sb2.Append(Get_LowerCase());
        }
        else if (Get_PwdMode() == PwdMode.Upper)
        {
            temp = r.Next(0, 3);
            if (temp == 0)
                sb2.Append(Get_Num());
            else if (temp == 1)
                sb2.Append(Get_LowerCase());
            else
                sb2.Append(Get_UpperCase());
        }
        else if (Get_PwdMode() == PwdMode.SpecialChar)
        {
            temp = r.Next(0, 3);
            if (temp == 0)
                sb2.Append(Get_Num());
            else if (temp == 1)
                sb2.Append(Get_LowerCase());
            else
                sb2.Append(Get_SpecialChar());
        }
        else if (Get_PwdMode() == PwdMode.UpperandSpecialChar)
        {
            temp = r.Next(0, 4);
            if (temp == 0)
                sb2.Append(Get_Num());
            else if (temp == 1)
                sb2.Append(Get_LowerCase());
            else if (temp == 2)
                sb2.Append(Get_UpperCase());
            else
                sb2.Append(Get_SpecialChar());
        }
    }

    this.Dispatcher.Invoke(() =>
                           this.txt_results.Text = sb2.ToString()
                          );
}

完整Demo下载

下载地址:https://xxing9199.lanzous.com/isPrji1f0dc

标签:sb2,temp,Get,C#,else,密码,PwdMode,随机,Append
来源: https://www.cnblogs.com/xxing9199/p/13924589.html