• 随机生成密码


    要求:

    1、总长8位;

    2、至少一个大写字母;

    3、至少一个小写字母;

    4、至少一个数字(0~9);

    5、至少一个特殊字符;

    using System;

    using System.Collections.Generic;

    using System.Text;

     

    /****************************************************

     * Author:quietwalk

     * Date:2011-12-02

     * Fuction:Generate a random password that consists of mixed case (at least one upper case and at least one lower case),

     * at least one special character( Symbol) ,

     * and at least one numeric, and eight characters long.

     * **************************************************/

     

    namespace UsefulClass

    {

        public class PasswordGenerator

        {

            //

            public string GeneratePassowrd()

            {

                long tick = DateTime.Now.Ticks;

                Random rdm = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));

     

                List<int> lsOrder = GetOrder();

     

                int maxNumber = 5;

                int iRemain = 4;

                int iCurrentLength = 0;

                int number = 0;

     

                List<int> lsResult = new List<int>();

     

                for (int i = 0; i < 4; i++)

                {

                    int type = lsOrder[i];//order of U,L,S,N

     

                    maxNumber = 8 - iCurrentLength - iRemain + 1;// 此次最多能取的个数 = 总长-已经取的长度-取的次数

                    if (i == 3)//最后一次全取

                    {

                        number = maxNumber;

                    }

                    else

                    {

                        number = rdm.Next(1, maxNumber);//此次实际要取的个数

                        iCurrentLength += number;//目前串的实际长度

                        iRemain = 4 - (i + 1);//剩余可取数

                    }

     

                    List<int> lsTemp = new List<int>();

                    switch (type)

                    {

                        case 1://Uper Case

                            lsTemp = GetSomeNumber('U', number);

                            break;

                        case 2://Lower Case

                            lsTemp = GetSomeNumber('L', number);

                            break;

                        case 3://Special Character

                            lsTemp = GetSomeNumber('S', number);

                            break;

                        case 4://Numeric

                            lsTemp = GetSomeNumber('N', number);

                            break;

                    }

                    foreach (int value in lsTemp)

                    {

                        lsResult.Add(value);

                    }

                }//for

     

                #region Re-Order

     

                long tick2 = DateTime.Now.Ticks;

                Random random = new Random((int)(tick2 & 0xffffffffL) | (int)(tick2 >> 32));

     

                //   Random random = new Random();

                int indexTemp = -1;

                List<int> mixOrder = new List<int>();

                for (int i = lsResult.Count - 1; i >= 0; i--)

                {

                    if (i != 0)

                    {

                        indexTemp = random.Next(i + 1);

                    }

                    else

                    {

                        indexTemp = 0;

                    }

                    mixOrder.Add(lsResult[indexTemp]);

                    lsResult.RemoveAt(indexTemp);

                }

                #endregion Re-Order


                string strResult = string.Empty;

                foreach (int value in mixOrder)

                {

                    strResult += Convert.ToChar(value);

                }

                return strResult;

            }

     

            private List<int> GetSomeNumber(char type, int number)

            {

                List<int> lsResult = new List<int>();

                long tick = DateTime.Now.Ticks;

                Random random = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));

     

                if (type == 'U')//Upper case

                {

                    #region Upper case

     

                    //List<int> lsUpperCase = new List<int>();

                    //for (int i = 65; i <= 90; i++)

                    //{

                    //    lsUpperCase.Add(i);

                    //}

                    // Random random = new Random();

     

                    int iStart = 65;//A

                    int iEnd = 90;//Z

                    for (int j = 0; j < number; j++)

                    {

                        int iResult = random.Next(iStart, iEnd + 1);

                        lsResult.Add(iResult);

                    }

     

                    #endregion

                }

                else if (type == 'L')//Lower case

                {

                    #region Lower case

     

                    //List<int> lsLowerCase = new List<int>();

                    //for (int i = 97; i <= 122; i++)

                    //{

                    //    lsLowerCase.Add(i);

                    //}

     

                    int iStart = 97;//a

                    int iEnd = 122;//z

                    for (int j = 0; j < number; j++)

                    {

                        int iResult = random.Next(iStart, iEnd + 1);

                        lsResult.Add(iResult);

                    }

     

                    #endregion

                }

                else if (type == 'N')//Number

                {

                    #region Number

     

                    //List<int> lsNumber = new List<int>();

                    //for (int i = 48; i <= 57; i++)

                    //{

                   //    lsNumber.Add(i);

                    //}

     

                    int iStart = 48;//0

                    int iEnd = 57;//9

                    for (int j = 0; j < number; j++)

                    {

                        int iResult = random.Next(iStart, iEnd + 1);

                        lsResult.Add(iResult);

                    }

     

                    #endregion

                }

                else if (type == 'S')//Special characters

                {

                    #region Sepcial Character

     

                    #region initialize the list

     

                    List<int> lsSpacialCahracters = new List<int>();

                    lsSpacialCahracters.Add(33);//!

                    lsSpacialCahracters.Add(34);//"

                    lsSpacialCahracters.Add(35);//#

                    lsSpacialCahracters.Add(36);//$

                    lsSpacialCahracters.Add(37);//%

                    lsSpacialCahracters.Add(38);//&

                    lsSpacialCahracters.Add(39);//,

                    lsSpacialCahracters.Add(40);//(

                    lsSpacialCahracters.Add(41);//)

                    lsSpacialCahracters.Add(42);//*

                    lsSpacialCahracters.Add(43);//+

                    lsSpacialCahracters.Add(44);//,

                    lsSpacialCahracters.Add(45);//-

                    lsSpacialCahracters.Add(46);//.

                    lsSpacialCahracters.Add(47);///

                    lsSpacialCahracters.Add(58);//:

                    lsSpacialCahracters.Add(59);//;

                    lsSpacialCahracters.Add(60);//<

                    lsSpacialCahracters.Add(61);//=

                    lsSpacialCahracters.Add(62);//>

                    lsSpacialCahracters.Add(63);//?

                    lsSpacialCahracters.Add(64);//@

                    lsSpacialCahracters.Add(91);//[

                    lsSpacialCahracters.Add(92);//\

                    lsSpacialCahracters.Add(93);//]

                    lsSpacialCahracters.Add(94);//^

                    lsSpacialCahracters.Add(95);//_

                    lsSpacialCahracters.Add(96);//¡é

                    lsSpacialCahracters.Add(123);//{

                    lsSpacialCahracters.Add(124);//|

                    lsSpacialCahracters.Add(125);//}

                    lsSpacialCahracters.Add(126);//~

     

                    #endregion initialize the list

     

                    int iTotalCount = lsSpacialCahracters.Count - 1;

                    //Random rdm = new Random();

                    for (int i = 0; i < number; i++)

                    {

                        int index = random.Next(iTotalCount + 1);

     

                        lsResult.Add(lsSpacialCahracters[index]);

                    }

     

                    #endregion Sepcial Character

                }

     

                return lsResult;

            }

     

            private List<int> GetOrder()

            {

                List<int> intList = new List<int>();

                intList.Add(1);//upper case

                intList.Add(2);//lower case

                intList.Add(3);//special character

                intList.Add(4);//numeric

     

                Random random = new Random();

                int index = -1;

                List<int> lsOrder = new List<int>();

     

                for (int i = intList.Count - 1; i >= 0; i--)

                {

                    if (i != 0)

                    {

                        index = random.Next(i + 1);

                    }

                    else

                    {

                        index = 0;

                    }

                    // strNumber += intList[index].ToString();

                    lsOrder.Add(intList[index]);

                    intList.RemoveAt(index);

                }

                return lsOrder;

            }

     

        }

    }

     

  • 相关阅读:
    【洛谷P4557】【JSOI2018】—战争(Minkowski Sum)
    【洛谷P4557】【JSOI2018】—战争(Minkowski Sum)
    【BZOJ3879】—SvT(后缀自动机+虚树/后缀自动机+单调栈)
    【BZOJ3879】—SvT(后缀自动机+虚树/后缀自动机+单调栈)
    多测师讲解pyhon__hashlib_高级讲师肖sir
    多测师讲解python函数 _open_高级讲师肖sir
    多测师讲解python函数 _zip_高级讲师肖sir
    多测师讲解内置函数 _format_高级讲师肖sir
    多测师讲解python _函数return_高级讲师肖sir
    多测师讲解python _函数中变量_高级讲师肖sir
  • 原文地址:https://www.cnblogs.com/quietwalk/p/2271789.html
Copyright © 2020-2023  润新知