gpt4 book ai didi

c# - 批量生成随 secret 码

转载 作者:太空狗 更新时间:2023-10-30 00:06:02 26 4
gpt4 key购买 nike

我使用此源代码生成随 secret 码:

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”循环语句中创建随 secret 码?

最佳答案

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 字符。

关于c# - 批量生成随 secret 码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6192496/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com