如何做随机密码[]数组 [英] how to do random password[] array

查看:73
本文介绍了如何做随机密码[]数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我再次想将我的password[] 数组随机化.
我尝试过,但是没有用,该怎么办?


Hello,

I want again, to randomise my password[] array.
I tried but it is not working, how do I do it?


static string MakePassword1(int pl)
        {                       
            char[] comb = new char[pl];
            string possibles = "ZYXWVUTSRQPNMLKJHGFEDCBA";
            char[] passwords = new char[pl];
            Random rd = new Random();
            string possibles1 = "0123456789";
            Random rd1 = new Random();
            string possibles2 = "abcdefghjkmnpqrstuvwxyz";
            
            Random rd2 = new Random();
            string possibles3 = "!@#$%^&*(?)_+?|~";
            Random rd3 = new Random();

            try
            {
                for (int k = 0; k <= pl; k = k + 4)
                {

                    passwords[k] = possibles[rd.Next(0, possibles.Length)];
                    passwords[k + 1] = possibles1[rd1.Next(0, possibles1.Length)];
                    passwords[k + 2] = possibles2[rd2.Next(0, possibles2.Length)];
                    passwords[k + 3] = possibles3[rd3.Next(0, possibles3.Length)];                  

                }
            }

            catch (IndexOutOfRangeException)
            {
                
                return new string(passwords);
            }
                   

            return new string(passwords);
        }

推荐答案

%^& *(?)_ +?|〜"; 随机rd3 = Random(); 尝试 { for ( int k = 0 ; k < = pl; k = k + 4 ) { 密码[k] =可能性[rd.Next( 0 ,可能性.长度)]; 密码[k + 1 ] = possibles1 [rd1.Next( 0 ,可能性s.Length)]; 密码[k + 2 ] = possibles2 [rd2.Next( 0 ,可能性s2.Length)]; 密码[k + 3 ] = possibles3 [rd3.Next( 0 ,可能性s3.Length)]; } } 捕获(IndexOutOfRangeException) { 返回 字符串(密码); } 返回 字符串(密码); }
%^&*(?)_+?|~"; Random rd3 = new Random(); try { for (int k = 0; k <= pl; k = k + 4) { passwords[k] = possibles[rd.Next(0, possibles.Length)]; passwords[k + 1] = possibles1[rd1.Next(0, possibles1.Length)]; passwords[k + 2] = possibles2[rd2.Next(0, possibles2.Length)]; passwords[k + 3] = possibles3[rd3.Next(0, possibles3.Length)]; } } catch (IndexOutOfRangeException) { return new string(passwords); } return new string(passwords); }


您可能想在这里看看一些替代方法-

http://www.obviex.com/Samples/Password.aspx [ http://madskristensen.net/post/Generate-random-password-in-C.aspx [^ ]
You might want to look at some alternate approaches here -

http://www.obviex.com/Samples/Password.aspx[^]
http://madskristensen.net/post/Generate-random-password-in-C.aspx[^]


代码非常糟糕.将所有可能"放入一个字符串中.不要抓异常!这是没有意义的,并且在调试代码时会给您带来噩梦.可以仅以范围(两个变量:最小和最大代码点)的形式定义可能性,这甚至会更加容易,因为您只需将随机生成的整数转换为字符,而不使用索引,这样会更好性能,因为您可以完全消除间接操作的一个步骤.

(请参见我的有关使用异常的说明,在这里:
我如何制作滚动条到达底部时将停止的循环 [当我运行应用程序时,异常是捕获了如何处理此问题? [
Code is pretty bad. Put all your "possibles" in one string. Do not catch exception! This is pointless and will give you a nightmare when you debug code. It it it possible to define possibles just in the form of range (two variables: minimum and maximum code point), would be even easier, as your would simply convert a randomly-generated integer to a character, not using indexes, which is better for performance as you would eliminate exactly one step of indirection.

(See my directions on using exceptions here:
How do i make a loop that will stop when a scrollbar reaches the bottom[^]
When i run an application an exception is caught how to handle this?[^])

In the loop, use 0 to <length, not <=length, use k++.

Use longer variable names, never of one character. Imagine how you do the search. Don''t use names ending with 1, 2, etc., rename all auto-generated names to somethings semantic. Use Microsoft naming conventions.

Use only one instance of Random, initialize it outside of your method, only once per application run-time.

Use Random.Next with one parameter: maxValue, because it''s always an index. Don''t make a mistake in maxValue: it''s not String.Length but String.Length - 1.

—SA


这篇关于如何做随机密码[]数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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