【问题标题】:How do I mix and generate random strings(password) in C# .NET如何在 C# .NET 中混合和生成随机字符串(密码)
【发布时间】:2016-09-16 23:30:49
【问题描述】:

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

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

我正在尝试混合所有的

公共常量字符串

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

是否有任何示例可以让我查看并尝试了解它们是如何制作的?我在 SO 上看到了一个用 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)
        {
        }
    }
}

【问题讨论】:

  • 您是否看过 Related 下的八个帖子中的 任何 个标题几乎相同的帖子?
  • lengthamount有什么区别?
  • @itsme86 Ammount = 它生成多少个密码

标签: c# string random passwords generator


【解决方案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 之类的东西。

【讨论】:

  • 我想我遗漏了一些东西。我只是稍微重做了代码,它显示错误 CS0103 The name 'length' does not exist in the current context
【解决方案2】:

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

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

查看http://keepass.info/download.html源中的文件 KeePassLib\Cryptography\PasswordGenerator

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-09
    • 2012-08-05
    • 2013-03-25
    • 2014-02-09
    • 2013-07-26
    • 2014-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多