使用线程生成密码 [英] password generation using threading

查看:89
本文介绍了使用线程生成密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我想通过线程来完成这项工作.帮帮我...

private static string MakePassword4(int pl)
       {
           string possibles =
           "0123456789";
           char[] passwords = new char[pl];
           Random rd = new Random();
           {
               for (int i = 0; i <1000; pl; i++)
               {
                   passwords[i] = possibles[rd.Next(0, possibles.Length)];
               }
           }
           return new string(passwords);
       }


private void button1_Click(object sender, EventArgs e)
 {
 

         listView1.View = View.Details;
         listView1.Items.Clear();
         for (int j = 0; j<1000;j++)         {
             temp = MakePassword11(length);
             Refresh();
             listView1.GridLines = true;
             listView1.Items.Add(temp.ToString());
         }
        return;
     }

[edit]Code block added - OriginalGriff[/edit]

解决方案

为什么要通过线程执行此操作?您认为这将花费大量时间吗?
如果您的MakePassword11方法类似于您的MakePassword4方法,那么设置和销毁新线程的开销将超过该方法应承担的开销-而且发生的事情还很遥远.

我还可以建议仅由数字组成的密码在不安全方面有点吗? IE.蛮力方法会很快将其破解...

您要达到什么目的?

顺便说一句:更改您的用户名:在公共论坛上发布您的电子邮件地址是垃圾邮件的邀请.给他们一个生成的GUID的子字符串?

 字符串密码= Guid.NewGuid().ToString("  N").Substring( 8 ) ; 



无需穿线.

编辑==============

对这个答案的1票是没有道理的.我的解决方案是 a) 快于原始问题中发布的内容,并且 b) 生成随机的STRONGER密码,因为它们可能包含字母字符和数字.

如果要证明使用Guid.NewGuid方法的随机性,请在WinForms应用程序中运行以下代码:

 私有 无效 CreateGuids()
{
    HashSet< string> hash =  HashSet< string>();
     for ( int  i =  0 ; i <   1000 ; i ++)
    {
        hash.Add(Guid.NewGuid().ToString(" ).Substring( 5  8 ));
    }
    MessageBox.Show(字符串 .Format(" ,hash.Count));
} 



无可否认,这比一次建立一个字符并为每个字符调用Random.Next更好.

hello, I want to do this work by threading. help me how...

private static string MakePassword4(int pl)
       {
           string possibles =
           "0123456789";
           char[] passwords = new char[pl];
           Random rd = new Random();
           {
               for (int i = 0; i <1000; pl; i++)
               {
                   passwords[i] = possibles[rd.Next(0, possibles.Length)];
               }
           }
           return new string(passwords);
       }


private void button1_Click(object sender, EventArgs e)
 {
 

         listView1.View = View.Details;
         listView1.Items.Clear();
         for (int j = 0; j<1000;j++)         {
             temp = MakePassword11(length);
             Refresh();
             listView1.GridLines = true;
             listView1.Items.Add(temp.ToString());
         }
        return;
     }

[edit]Code block added - OriginalGriff[/edit]

解决方案

Why would you want to do that via threading? Do you think it is going to take considerable amounts of time?
If your MakePassword11 method is anything like your MakePassword4 method, then there will be more overhead in setting up and destroying the new thread than should be taken up by the method - and it will be nowhere near as clear what is happening.

Can I also suggest that a password which consists only of numeric digits is a bit on the insecure side? I.e. a brute force approach will crack it pretty quickly...

What are you trying to achieve?

BTW: Change your UserName: posting your email address to public forums is an invitation to spam...


Instead of going through all that, why don''t you just create a GUID and give them a substring of the generated GUID?

string password = Guid.NewGuid().ToString("N").Substring(5, 8);



No threading is needed.

EDIT ==============

The 1 vote on this answer is unjustified. My solution is a) Faster than the what was posted in the original question, and b) generates STRONGER random passwords because they may include alphabetic characters as well as numbers.

If you want proof of the randomness of using the Guid.NewGuid method, run this code in a WinForms app:

private void CreateGuids()
{
    HashSet<string> hash = new HashSet<string>();
    for (int i = 0; i < 1000; i++)
    {
        hash.Add(Guid.NewGuid().ToString("N").Substring(5,8));
    }
    MessageBox.Show(string.Format("{0} unique passwords generated.", hash.Count));
}



There is no denying that this is a better solution than building a password one character at a time, and calling Random.Next for each character.


这篇关于使用线程生成密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆