简体   繁体   English

C#:如何生成长度为7或8个字符的唯一密码

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

I need to repeated generate unique password many times, Ensure that every time the generated passwords are unique, Please help me. 我需要多次重复生成唯一密码,确保每次生成的密码都是唯一的,请帮帮我。

Thanks! 谢谢!

If you want to generate uniq password every time than 如果你想每次都生成uniq密码

take CurrentTIME and CurrrentDATE in account because by this you can able to create new password. 考虑到CurrentTIME和CurrrentDATE,因为这样你就可以创建新的密码。

have look to this resolve your problem : generating a batch of random passwords 看看这解决了你的问题: 生成一批随机密码

So here is another method which generates cryptedRandom password and a thread safe... 所以这是生成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;
        }

    }

Now you can call this method like this: - 现在你可以像这样调用这个方法: -

string GeneratedPassword = "";

GeneratedPassword = CryptedRandomString() + CryptedRandomString();

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

Now you all must be wondering why GeneratedPassword = CryptedRandomString() + CryptedRandomString(); 现在你们都必须想知道为什么GeneratedPassword = CryptedRandomString() + CryptedRandomString(); , the reason I called CryptedRamdomString() method twice is just to make sure it returns more then 10 digits so as it will be easier to get eight character passwords otherwise if it is called once then sometimes it will generate less then eight character password. ,我之前两次调用CryptedRamdomString()方法的原因只是为了确保它返回10个以上的数字,因为它更容易获得8个字符密码,否则如果它被调用一次,那么有时它会产生少于8个字符的密码。

Well you must consider one thing before using this method that generating random numbers using "RNGCryptoServiceProvider " is bit time consuming then Random.Next. 那么在使用这种使用“RNGCryptoServiceProvider”生成随机数的方法之前你必须考虑一件事,比Random.Next有点耗时。 But "RNGCryptoServiceProvider " is much more secure then "Random.Next" . 但是“RNGCryptoServiceProvider”比“Random.Next”更安全。

I'd define an array of possible characters for the password alphabet, and then generate 7 or 8 random indexes into that array to create a password. 我为密码字母表定义了一组可能的字符,然后在该数组中生成7或8个随机索引以创建密码。

This would need to be refined if you want to guarantee a minimum number of each character type, etc. 如果你想保证每个字符类型的最小数量等,这将需要改进。

你说全球独特的字符串?

System.Guid.NewGuid().ToString()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM