此代码中的唯一字符密码生成

编程入门 行业动态 更新时间:2024-10-27 00:24:14
本文介绍了此代码中的唯一字符密码生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好,我使用此代码生成密码,但是我想要唯一的字符密码.密码中的字符必须唯一.请帮助我

私有 静态 字符串 MakePassword( int pl) { 字符 [] comb = 新 字符 [ pl]; 字符串可能= " ZYXWVUTSRQPONMLKJIHGFEDCAB" ; 字符 []密码= 新 字符 [ pl]; 随机rd = 新 Random(); 尝试 { 字符 ss =可能性[rd.Next( 0 ,可能性.长度)]; char sp =可能性[rd.Next( 0 ,可能性.长度)]; 如果(ss!= sp) { 字符 a = sp; } 密码[ 1 ] =可能[rd.Next( 0 ,可能.长度)]; 密码[ 2 ] =可能[rd.Next( 0 ,可能.长度)]; 密码[ 3 ] =可能[rd.Next( 0 ,可能.长度)]; 密码[ 4 ] =可能[rd.Next( 0 ,可能.长度)]; 密码[ 5 ] =可能[rd.Next( 0 ,可能.长度)]; 密码[ 6 ] =可能[rd.Next( 0 ,可能.长度)]; 密码[ 7 ] =可能[rd.Next( 0 ,可能.长度)]; 密码[ 8 ] =可能[rd.Next( 0 ,可能.长度)]; 密码[ 9 ] =可能[rd.Next( 0 ,可能.长度)]; 密码[ 10 ] =可能[rd.Next( 0 ,可能.长度)]; 密码[ 11 ] =可能[rd.Next( 0 ,可能.长度)]; 密码[ 12 ] =可能[rd.Next( 0 ,可能.长度)]; 密码[ 13 ] =可能[rd.Next( 0 ,可能.长度)]; passwords [ 14 ] =可能[rd.Next( 0 ,可能.长度)]; 密码[ 15 ] =可能[rd.Next( 0 ,可能.长度)]; 密码[ 16 ] =可能[rd.Next( 0 ,可能.长度)]; 密码[ 17 ] =可能[rd.Next( 0 ,可能.长度)]; 密码[ 18 ] =可能[rd.Next( 0 ,可能.长度)]; passwords [ 19 ] =可能[rd.Next( 0 ,可能.长度)]; 密码[ 20 ] =可能[rd.Next( 0 ,可能.长度)]; passwords [ 21 ] =可能[rd.Next( 0 ,可能.长度)]; passwords [ 22 ] =可能[rd.Next( 0 ,可能.长度)]; 密码[ 23 ] =可能[rd.Next( 0 ,可能.长度)]; } 捕获(IndexOutOfRangeException e) { for ( int j = 0 ; j < pl; j ++) { comb [j] =密码[j]; } } 返回 新 字符串(comb); }

解决方案

已选择一个字符,需要将其从剩余可用字符列表中删除. 我建议您使用0到25的字符串数组,应用随机因子,然后删除.测试以查看是否已选择该图形,如果是,请再次运行随机数,否则请选择它. 这是相当简单的东西,但是请尝试一下,如果遇到困难,我们可以为您提供进一步的帮助.

看看这篇文章 AC#密码生成器 [ ^ ] 它具有某些字段,可让您控制密码的生成 *排除项:指定在密码生成中要排除的字符集. * Minimum:指定所生成密码的最小长度. * Maximum:指定所生成密码的最大长度. * ConsecutiveCharacters:控制在生成的密码中连续字符的生成. * RepeatingCharacters:控制在生成的密码中重复字符的生成. * ExcludeSymbols:从用于生成密码的字符集中排除符号 使用ConsecutiveCharacters和RepeatingCharacters属性应该可以满足您的要求.您没有使用任何密码,因为您允许自己重复passwords[1] = possibles[rd.Next(0, possibles.Length)] 23次. 您是否听说过DRY原理, en.wikipedia/wiki/DRY [ ^ ],也许在编程中排名第一? 认真地学习什么是循环,异常(方法,参数,变量). 您的代码中存在循环的事实并不意味着您知道它的用途. 阅读此内容: 我的程序有问题.请帮忙! [ ^ ] 这让我有些清醒: norvig/21-days.html [ ^ ]. 祝你好运,
—SA

hello, Im using this code for password generation, but i want unique character password. character must be unique in a password.plz help me

private static string MakePassword(int pl) { char[] comb = new char[pl]; string possibles = "ZYXWVUTSRQPONMLKJIHGFEDCAB"; char[] passwords = new char[pl]; Random rd = new Random(); try { char ss = possibles[rd.Next(0, possibles.Length)]; char sp= possibles[rd.Next(0, possibles.Length)]; if (ss != sp) { char a = sp; } passwords[1] = possibles[rd.Next(0, possibles.Length)]; passwords[2] = possibles[rd.Next(0, possibles.Length)]; passwords[3] = possibles[rd.Next(0, possibles.Length)]; passwords[4] = possibles[rd.Next(0, possibles.Length)]; passwords[5] = possibles[rd.Next(0, possibles.Length)]; passwords[6] = possibles[rd.Next(0, possibles.Length)]; passwords[7] = possibles[rd.Next(0, possibles.Length)]; passwords[8] = possibles[rd.Next(0, possibles.Length)]; passwords[9] = possibles[rd.Next(0, possibles.Length)]; passwords[10] = possibles[rd.Next(0, possibles.Length)]; passwords[11] = possibles[rd.Next(0, possibles.Length)]; passwords[12] = possibles[rd.Next(0, possibles.Length)]; passwords[13] = possibles[rd.Next(0, possibles.Length)]; passwords[14] = possibles[rd.Next(0, possibles.Length)]; passwords[15] = possibles[rd.Next(0, possibles.Length)]; passwords[16] = possibles[rd.Next(0, possibles.Length)]; passwords[17] = possibles[rd.Next(0, possibles.Length)]; passwords[18] = possibles[rd.Next(0, possibles.Length)]; passwords[19] = possibles[rd.Next(0, possibles.Length)]; passwords[20] = possibles[rd.Next(0, possibles.Length)]; passwords[21] = possibles[rd.Next(0, possibles.Length)]; passwords[22] = possibles[rd.Next(0, possibles.Length)]; passwords[23] = possibles[rd.Next(0, possibles.Length)]; } catch (IndexOutOfRangeException e) { for (int j = 0; j < pl; j++) { comb[j] = passwords[j]; } } return new string(comb); }

解决方案

One a character has been selected, it needs to be removed from the list of remaining available characters. I would suggest you have an string array 0 to 25, apply the random factor, and, remove. Test to see if that figure has been selected before, if so, run the random again, and if not, select it. This is fairly simple stuff, but try it, and if you run into difficulties we can help you further.

Have a look at this article A C# Password Generator[^] This has certain fields that let you control the password generation * Exclusions: Specifies the set of characters to exclude in password generation. * Minimum: Specifies the minimum length of the generated password. * Maximum: Specifies the maximum length of the generated password. * ConsecutiveCharacters: Controls generation of consecutive characters in the generated password. * RepeatingCharacters: Controls generation of repeating characters in the generated password. * ExcludeSymbols: Excludes symbols from the set of characters used to generate the password Use of the ConsecutiveCharacters and RepeatingCharacters properties should let you achieve what you require

I have only one advice: stop generating passwords; there is no use in any passwords for you is you allow yourself to repeat passwords[1] = possibles[rd.Next(0, possibles.Length)] 23 times. Have you ever heard of the DRY principle, en.wikipedia/wiki/DRY[^], maybe #1 in programming? Seriously, learn what are loops, exceptions (methods, parameters, variables). The fact there is a loop in your code doesn''t mean you have an idea what is it for. Read this: I have a problem with my program. Please help![^] And this is for some sobering: norvig/21-days.html[^]. Good luck and best wishes,
—SA

更多推荐

此代码中的唯一字符密码生成

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

发布评论

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

>www.elefans.com

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