gpt4 book ai didi

C# : How to generate unique passwords with the length of 7 or 8 characters

转载 作者:太空狗 更新时间:2023-10-29 22:55:00 28 4
gpt4 key购买 nike

我需要多次重复生成唯一密码,请确保每次生成的密码都是唯一的,请帮助我。

谢谢!

最佳答案

所以这是另一种生成 cryptedRandom 密码和线程安全的方法...

    private string CryptedRandomString()
{
lock (this)
{
int rand = 0;

byte[] randomNumber = new byte[5];

RNGCryptoServiceProvider Gen = new RNGCryptoServiceProvider();
Gen.GetBytes(randomNumber);

rand = Math.Abs(BitConverter.ToInt32(randomNumber, 0));

return ConvertIntToStr(rand);
}
}

private string ConvertIntToStr(int input)
{
lock (this)
{
string output = "";
while (input > 0)
{
int current = input % 10;
input /= 10;

if (current == 0)
current = 10;

output = (char)((char)'A' + (current - 1)) + output;
}
return output;
}

}

现在你可以像这样调用这个方法:-

string GeneratedPassword = "";

GeneratedPassword = CryptedRandomString() + CryptedRandomString();

Console.WriteLine(GeneratedPassword.Substring(0,8));

现在你们一定想知道为什么 GeneratedPassword = CryptedRandomString() + CryptedRandomString(); ,我两次调用 CryptedRamdomString() 方法的原因只是为了确保它返回超过 10 位数字,以便获取八个字符的密码会更容易,否则如果调用一次,有时会生成少于八个字符的密码。

在使用此方法之前,您必须考虑一件事,即使用“RNGCryptoServiceProvider”生成随机数比 Random.Next 更耗时。但是“RNGCryptoServiceProvider”比“Random.Next”安全得多。

关于C# : How to generate unique passwords with the length of 7 or 8 characters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5576191/

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