C#随机密码生成器 [英] C# Random Password Generator

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

问题描述

下面是代码:(passwordLengthBox是一个的NumericUpDown箱,R和k是随机数)

Here is the code: (passwordLengthBox is a NumericUpDown Box, r and k are random numbers)

    private void generateButton_Click(object sender, EventArgs e)
            {
                int r, k;
                int passwordLength = (Int32)passwordLengthBox.Value;
                string password = "";
                char[] upperCase = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
                char[] lowerCase = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
                int[] numbers = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
                Random rRandom = new Random();



                for (int i = 0; i < passwordLength; i++)
                {
                    r = rRandom.Next(3);

                    if (r == 0)
                    {
                        k = rRandom.Next(0, 25);
                        password += upperCase[k];
                    }

                    else if (r == 1)
                    {
                        k = rRandom.Next(0, 25);
                        password += lowerCase[k];
                    }

                    else if (r == 2)
                    {
                        k = rRandom.Next(0, 9);
                        password += numbers[k];
                    }

                }

                textBox.Text = password;
            }

这程序做什么是创建字母(包括大写一个随机密码和小写)和数字在我选择的长度。
的问题是,该程序的功能的不可以使密码的长度,因为我选择了

What this program does is to create a random password with letters (both upper case and lower case) and numbers at the length that I choose. The problem is that the program does not make the password length as I chose.

有关为例:5,如果我输入该箱的NumericUpDown(passwordLengthBox),设置密码的长度,有时它给我的是5个字符长和某个字符6/7/8长密码的密码。

For exemple: if I type 5 in the NumericUpDown Box (passwordLengthBox) that sets the Password Length sometimes its giving me passwords that are 5 chars long and sometime 6/7/8 chars long passwords.

什么是我的?错

推荐答案

问题就在这里:

int[] numbers = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };



该宣言的每一个数字追加到时间密码它被作为ASCII数字,而不是一个真正的价值。所以,你要添加的整数从48到57,是什么让结果字符串比预期长。

With that declaration every time a number is appended into password it is taken as ASCII number, not a real value. So you're adding integers from 48 to 57, what makes result string longer then expected.

例如。当一个随机数生成 6 ,要追加类似:((int)的'6')的ToString()密码变量,究竟增加了 54 而不是 6

e.g. when 6 is generated as a random number, you're appending something like: ((int)'6').ToString() into your password variable, what actually adds 54 instead of 6.

声明该数组为的char [] ,它会正常工作。

Declare that array as char[] and it will works fine.

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

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