C#随机密码生成器

编程入门 行业动态 更新时间:2024-10-27 06:25:20
本文介绍了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#随机密码生成器

本文发布于:2023-11-10 09:20:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574982.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:生成器   密码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!