批量生成随机密码

发布于 2024-11-10 14:05:05 字数 793 浏览 3 评论 0原文

我使用这个源代码来生成随机密码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

静赏你的温柔 2024-11-17 14:05:06

GUID 不是随机的,它们只是唯一的(在单个系统内)。即使随机数生成器也有限制,它将返回的最小值和最大值,并且真正的随机意味着您可以一遍又一遍地获得相同的结果,您只是无法分辨。

您确定您的意思是随机,而不是强吗?

XKCD
http://xkcd.com/221/

好的,现在我们知道您想要什么 500 - 1000 个独特的密码。我怀疑是否需要唯一性,因为我假设它们是用于用户帐户的,但是......(在没有方便的VS的情况下输入)

List<string> passwords = new List<string>();

while (passwords.Length < 1000)
{
    string generated = System.Web.Security.Membership.GeneratePassword(
                           10, // maximum length
                           3)  // number of non-ASCII characters.
    if (!passwords.Contains(generated))
        passwords.Add(generated);
}

然后你将得到一个包含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?

XKCD
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)

List<string> passwords = new List<string>();

while (passwords.Length < 1000)
{
    string generated = System.Web.Security.Membership.GeneratePassword(
                           10, // maximum length
                           3)  // number of non-ASCII characters.
    if (!passwords.Contains(generated))
        passwords.Add(generated);
}

And then you'll have a list of 1000 unique passwords, which have a maximum of 10 characters, and 3 non-ASCII characters.

兔姬 2024-11-17 14:05:06

这并不是具体问题的答案,但这就是您的 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

夜未央樱花落 2024-11-17 14:05:06

如果您要在构建中生成随机密码,我强烈建议不要使用“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.

烟雨扶苏 2024-11-17 14:05:06

讽刺的是,如果您使用 GUID 的最后一个字符而不是第一个字符,您会得到更好的结果。

要回答你的问题,这样的事情就足够了:

private static Random rng=new Random();
private static string PasswordAlphabet="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

public string GetRandomPasswordUsingGUID(int length)
{
  string result="";

  while(length-->0)
    result+=PasswordAlphabet[rng.Next(PasswordAlphabet.Length)];

  return result;
}

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:

private static Random rng=new Random();
private static string PasswordAlphabet="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

public string GetRandomPasswordUsingGUID(int length)
{
  string result="";

  while(length-->0)
    result+=PasswordAlphabet[rng.Next(PasswordAlphabet.Length)];

  return result;
}
城歌 2024-11-17 14:05:06

您可以使用 Asp.net 的 Membership 类,它内置了一个密码生成器。它位于 System.Web dll 的 System.Web.Security 命名空间中。

// Generate a new 12-character password with 1 non-alphanumeric character.
  string password = Membership.GeneratePassword(12, 1);

更多详细信息请参见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.

// Generate a new 12-character password with 1 non-alphanumeric character.
  string password = Membership.GeneratePassword(12, 1);

More details here on MSDN: Membership.GeneratePassword Method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文