批量生成随机密码
我使用这个源代码来生成随机密码:
public string GetRandomPasswordUsingGUID(int length)
{
// Get the GUID
string guidResult = System.Guid.NewGuid().ToString();
// Remove the hyphens
guidResult = guidResult.Replace("-", string.Empty);
// Make sure length is valid
if (length <= 0 || length > guidResult.Length)
throw new ArgumentException("Length must be between 1 and " + guidResult.Length);
// Return the first length bytes
return guidResult.Substring(0, length).ToUpper();
}
当您调用该方法时它工作正常,但在“for”循环语句中则不然。
在这种情况下,它会生成一些重复的密码,这是错误的。
例如这样:
A4MNB597D7
AMGJCCC902
AWJ80CF6HX
A78EDJECIW
A78EDJECIW
A78EDJECIW
A78EDJECIW
A78EDJECIW
A2LYJCH23N
A2LYJCH23N
我如何在“For”循环语句中创建随机密码?
I use this source code for generating random passwords :
public string GetRandomPasswordUsingGUID(int length)
{
// Get the GUID
string guidResult = System.Guid.NewGuid().ToString();
// Remove the hyphens
guidResult = guidResult.Replace("-", string.Empty);
// Make sure length is valid
if (length <= 0 || length > guidResult.Length)
throw new ArgumentException("Length must be between 1 and " + guidResult.Length);
// Return the first length bytes
return guidResult.Substring(0, length).ToUpper();
}
It works fine when you invoke the method ,But not in "for" loop statement .
At this case it generate some repeated password which is wrong .
for example like this :
A4MNB597D7
AMGJCCC902
AWJ80CF6HX
A78EDJECIW
A78EDJECIW
A78EDJECIW
A78EDJECIW
A78EDJECIW
A2LYJCH23N
A2LYJCH23N
How can i create random password in "For" loop statement ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
GUID 不是随机的,它们只是唯一的(在单个系统内)。即使随机数生成器也有限制,它将返回的最小值和最大值,并且真正的随机意味着您可以一遍又一遍地获得相同的结果,您只是无法分辨。
您确定您的意思是随机,而不是强吗?
http://xkcd.com/221/
好的,现在我们知道您想要什么 500 - 1000 个独特的密码。我怀疑是否需要唯一性,因为我假设它们是用于用户帐户的,但是......(在没有方便的VS的情况下输入)
然后你将得到一个包含1000个唯一密码的列表,其中最多有10 个字符和 3 个非 ASCII 字符。
GUIDs are not random, they are only unique (within a single system). Even a random number generator has limits on it, the minimum and maximum values it will return, and being truly random means you could get the same result over and over again, you just can't tell.
Are you sure you mean random, as opposed to strong?
http://xkcd.com/221/
Ok, so now we have some idea of what you want 500 -1000 unique passwords. I'd question the need for uniqueness, as I would presume that they're for a user account, however ... (entered without VS handy)
And then you'll have a list of 1000 unique passwords, which have a maximum of 10 characters, and 3 non-ASCII characters.
这并不是具体问题的答案,但这就是您的 GUID 解决方案不起作用的原因:
http://blogs.msdn.com/b/oldnewthing/archive/2008/06/27/8659071.aspx
This is not an answer to the question specifically, but it is why your GUID solution will not work:
http://blogs.msdn.com/b/oldnewthing/archive/2008/06/27/8659071.aspx
如果您要在构建中生成随机密码,我强烈建议不要使用“NewGuid()”,因为根据用于创建 UUID 片段的生成算法,它们基于唯一的约 100 毫秒时间戳。
看看:
http://en.wikipedia.org/wiki/Universally_unique_identifier
你会更好创建允许字符的查找表并使用静态“随机”对象并根据生成的随机数将字符索引到表中。
If you're going to generate random passwords in build I would strongly recommend not using "NewGuid()" because based on the generation algorithm for creating the UUIDs segments of them are based on a unique ~100ms timestamp.
Look at:
http://en.wikipedia.org/wiki/Universally_unique_identifier
You would be better off creating a look-up table of allowed characters and using a static "Random" object and indexing characters into the table based on the random number generated.
讽刺的是,如果您使用 GUID 的最后一个字符而不是第一个字符,您会得到更好的结果。
要回答你的问题,这样的事情就足够了:
Ironically, you'd have had better results if you used the last characters of your GUID rather than the first.
To answer your question, something like this would suffice:
您可以使用 Asp.net 的 Membership 类,它内置了一个密码生成器。它位于 System.Web dll 的 System.Web.Security 命名空间中。
更多详细信息请参见MSDN:Membership.GeneratePassword 方法< /a>
You could use Asp.net's Membership class, which has a password generator built in. It's in the System.Web.Security namespace in the System.Web dll.
More details here on MSDN: Membership.GeneratePassword Method