如何在C#.NET中混合并生成随机字符串(密码)

时间:2016-05-19 21:18:52

标签: c# string random passwords generator

我正在学习C#并且我正在尝试创建一个随机字符串(密码)生成器..我觉得它很有趣,因为它不是简单的Hello World应用程序

我在Windows窗体应用程序中执行此操作

我正在尝试混合所有

  

public const string

并将其打印到我项目中的TextBox2 .. 但问题是我不知道如何实际混合它们我正在考虑for循环并检查每个角色..不确定我会怎么做..

有没有人有任何我可以查看的例子,并试图了解它们是如何制作的?我在这里看到了一个用Linq制作的,但是我无法理解它,因为我试图涉及复选框

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace RandomCHaracterGenerator
{
    public partial class Form1 : Form    
    {
        public Form1()
        {
            InitializeComponent();
        }

        class RandomPWDGenerator
        {
            public const string CapitilizedLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            public const string NonCapitilizedLetters = "abcdefghijklmnopqrstuvwxyz";
            public const string Numbers = "0123456789z";
            public const string SpecialCharacters = "!@#$%^*()_+";

            private static Random rnd = new Random();        
        }

        static void Main()
        {
            StringBuilder password = new StringBuilder();
            for (int i = 1; i <= 2; i++)
            {

            }
        }

        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HT_CAPTION = 0x2;

        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd,
                         int Msg, int wParam, int lParam);
        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();

        private void label1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void topPanel_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }

        private void generateLbl_MouseEnter(object sender, EventArgs e)
        {
            generatePanel.BackColor = System.Drawing.ColorTranslator.FromHtml("#4d4d4d");
        }

        private void generateLbl_MouseLeave(object sender, EventArgs e)
        {
            generatePanel.BackColor = Color.Transparent;
        }

        private void generateLbl_Click(object sender, EventArgs e)
        {
        }
    }
}

enter image description here

2 个答案:

答案 0 :(得分:1)

如果选中了复选框,您可以将字符串放入列表中:

List<string> charSets = new List<string>();

if (cbLowercase.Checked)
    charSets.Add(NonCapitilizedLetters);
if (cbUppercase.Checked)
    charSets.Add(CapitilizedLetters);
if (cbNumbers.Checked)
    charSets.Add(Numbers);
if (cbSpecial.Checked)
    charSets.Add(SpecialCharacters);

if (charSets.Count < 1)
    // Tell them they need to check at least 1 box or whatever

int length = int.Parse(txtLength.Text);
StringBuilder sb = new StringBuilder();

while (lenth-- > 0)
{
    int charSet = random.Next(charSets.Count);
    int index = random.Next(charSets[charSet].Length);
    sb.Append(charSets[charSet][index]);
}

string password = sb.ToString();
  • 免责声明:创建加密强密码时应避免使用Random。应该使用像RNGCryptoServiceProvider这样的东西。

答案 1 :(得分:0)

学习的一个好方法是查看真正的密码生成。

您可以使用开源密码管理器Keepass,看看那里发生了什么。

请参阅来自documentation

的来源中的文件KeePassLib \ Cryptography \ PasswordGenerator