csharp 随机密码生成器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 随机密码生成器相关的知识,希望对你有一定的参考价值。

using System;
using System.Linq;
using System.Text;

namespace LServFtpPwChanger
{
    public interface IPasswordGenerator
    {
        bool AllowConsecutiveCharacters { get; set; }
        bool AllowRepeatCharacters { get; set; }
        char[] AlphaChars { get; set; }
        char[] Exclusions { get; set; }
        char[] NumericChars { get; set; }
        char[] SpecialChars { get; set; }

        string Generate(int pwdLength);
    }
    
    public class PasswordGenerator
    {
        public bool AllowRepeatCharacters { get; set; }
        public bool AllowConsecutiveCharacters { get; set; }
        public char[] AlphaChars { get; set; }
        public char[] NumericChars { get; set; }
        public char[] SpecialChars { get; set; }
        public char[] Exclusions { get; set; }


        private readonly Random _random;
        private const string AlphaSet = "abcdefghijklmnopqrstuvwxyz";
        private const string NumberSet = "1234567890";
        private const string SpecialSet = "!@#$%^&*()_+=`";

        public PasswordGenerator()
        {
            _random = new Random();
            AllowConsecutiveCharacters = true;
            AllowRepeatCharacters = true;

            AlphaChars = AlphaSet.ToCharArray();
            NumericChars = NumberSet.ToCharArray();
            SpecialChars = SpecialSet.ToCharArray();
        }

        public PasswordGenerator(char[] alphaChars, char[] numericChars, char[] specialChars, char[] excludeChars)
        {
            _random = new Random();
            AllowConsecutiveCharacters = false;
            AllowRepeatCharacters = false;

            AlphaChars = alphaChars;
            NumericChars = numericChars;
            SpecialChars = specialChars;
            Exclusions = excludeChars;
        }

        private char GetRandomCharacter(char[] charArray)
        {
            var randomCharPosition = _random.Next(charArray.Length);
            var randomChar = charArray[randomCharPosition];
            return randomChar;
        }

        public string Generate(int pwdLength)
        {
            var pwdBuffer = new StringBuilder();
            var lastCharacter = '\n';

            var values = Enumerable.Range(0, pwdLength).OrderBy(x => _random.Next()).ToArray();
            var upperIndex = values[0];

            var numIndex = -99;
            if (NumericChars.Length > 0)
                numIndex = values[1];

            var specIndex = -98;
            if (NumericChars.Length > 0)
                specIndex = values[2];

            for (var i = 0; i < pwdLength; i++)
            {
                char[] arrayToPullFrom;
                if (i == numIndex)
                    arrayToPullFrom = NumericChars;
                else if (i == specIndex)
                    arrayToPullFrom = SpecialChars;
                else
                    arrayToPullFrom = AlphaChars;

                var nextCharacter = GetRandomCharacter(arrayToPullFrom);

                if (!AllowConsecutiveCharacters)
                {
                    while (char.ToUpper(lastCharacter) == char.ToUpper(nextCharacter))
                    {
                        nextCharacter = GetRandomCharacter(arrayToPullFrom);
                    }
                }

                if (!AllowRepeatCharacters)
                {
                    var temp = pwdBuffer.ToString();
                    var duplicateIndex = temp.IndexOf(nextCharacter.ToString(), StringComparison.CurrentCultureIgnoreCase);
                    while (-1 != duplicateIndex)
                    {
                        nextCharacter = GetRandomCharacter(arrayToPullFrom);
                        duplicateIndex = temp.IndexOf(nextCharacter);
                    }
                }

                if ((null != Exclusions))
                {
                    while (Exclusions.Any(c => char.ToUpper(c) == char.ToUpper(nextCharacter)))
                    {
                        nextCharacter = GetRandomCharacter(arrayToPullFrom);
                    }
                }

                if (i == upperIndex)
                    nextCharacter = char.ToUpper(nextCharacter);

                pwdBuffer.Append(nextCharacter);
                lastCharacter = nextCharacter;
            }

            return pwdBuffer.ToString();
        }


    }
}

以上是关于csharp 随机密码生成器的主要内容,如果未能解决你的问题,请参考以下文章

csharp 如何生成具有规则的随机密码的示例。从计算机编程基础知识到C#http://www.introprogramming.i

csharp 如何生成具有规则的随机密码的示例。从计算机编程基础知识到C#http://www.introprogramming.i

csharp C#中的密码安全随机数(范围:0到4,294,967,295)

csharp 随机字符串生成器和支付ReferenceNumber生成器

csharp MD5哈希和随机字符串生成器

csharp 生成真正的随机整数