C#中一些通用函數

//生成各種隨機密碼,包括純字母,純數字,帶特殊字符等,除非字母大寫密碼類型,其他方式都將採用小寫密碼

static string MakeRandomPassword(string pwdType, int length)
        {
            //定義密碼字符的範圍,小寫,大寫字母,數字以及特殊字符
            string lowerChars = "abcdefghijklmnopqrstuvwxyz";
            string upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            string numnberChars = "0123456789";
            string specialCahrs = "~!@#$%^*()_+|-=,./[]{}:;':";   //"\" 轉義字符不添加 "號不添加

            string tmpStr = "";

            int iRandNum;
            Random rnd = new Random();

            length = (length < 6) ? 6 : length; //密碼長度必須大於6,不然自動取6

            // LOWER爲小寫 UPPER爲大寫 NUMBER爲數字 NUMCHAR爲數字和字母 ALL所有包含 五種方式
            //只有當選擇UPPER纔會有大寫字母產生,其他方式中的字母都爲小寫,避免有些時候字母不區分大小寫
            if (pwdType == "LOWER")
            {
                for (int i = 0; i < length; i++)
                {
                    iRandNum = rnd.Next(lowerChars.Length);
                    tmpStr += lowerChars[iRandNum];
                }
            }
            else if (pwdType == "UPPER")
            {
                for (int i = 0; i < length; i++)
                {
                    iRandNum = rnd.Next(upperChars.Length);
                    tmpStr += upperChars[iRandNum];
                }
            }
            else if (pwdType == "NUMBER")
            {
                for (int i = 0; i < length; i++)
                {
                    iRandNum = rnd.Next(numnberChars.Length);
                    tmpStr += numnberChars[iRandNum];
                }
            }
            else if (pwdType == "NUMCHAR")
            {
                int numLength = rnd.Next(length);
                //去掉隨機數爲0的狀況
                if (numLength == 0)
                {
                    numLength = 1;
                }
                int charLength = length - numLength;
                string rndStr = "";
                for (int i = 0; i < numLength; i++)
                {
                    iRandNum = rnd.Next(numnberChars.Length);
                    tmpStr += numnberChars[iRandNum];
                }
                for (int i = 0; i < charLength; i++)
                {
                    iRandNum = rnd.Next(lowerChars.Length);
                    tmpStr += lowerChars[iRandNum];
                }
                //將取得的字符串隨機打亂
                for (int i = 0; i < length; i++)
                {
                    int n = rnd.Next(tmpStr.Length);
                    //去除n隨機爲0的狀況
                    //n = (n == 0) ? 1 : n;
                    rndStr += tmpStr[n];
                    tmpStr = tmpStr.Remove(n, 1);
                }
                tmpStr = rndStr;
            }
            else if (pwdType == "ALL")
            {
                int numLength = rnd.Next(length - 1);
                //去掉隨機數爲0的狀況
                if (numLength == 0)
                {
                    numLength = 1;
                }
                int charLength = rnd.Next(length - numLength);
                if (charLength == 0)
                {
                    charLength = 1;
                }
                int specCharLength = length - numLength - charLength;
                string rndStr = "";
                for (int i = 0; i < numLength; i++)
                {
                    iRandNum = rnd.Next(numnberChars.Length);
                    tmpStr += numnberChars[iRandNum];
                }
                for (int i = 0; i < charLength; i++)
                {
                    iRandNum = rnd.Next(lowerChars.Length);
                    tmpStr += lowerChars[iRandNum];
                }
                for (int i = 0; i < specCharLength; i++)
                {
                    iRandNum = rnd.Next(specialCahrs.Length);
                    tmpStr += specialCahrs[iRandNum];
                }
                //將取得的字符串隨機打亂
                for (int i = 0; i < length; i++)
                {
                    int n = rnd.Next(tmpStr.Length);
                    //去除n隨機爲0的狀況
                    //n = (n == 0) ? 1 : n;
                    rndStr += tmpStr[n];
                    tmpStr = tmpStr.Remove(n, 1);
                }
                tmpStr = rndStr;
            }
            //默認將返回數字類型的密碼
            else
            {
                for (int i = 0; i < length; i++)
                {
                    iRandNum = rnd.Next(numnberChars.Length);
                    tmpStr += numnberChars[iRandNum];
                }
            }
            return tmpStr;
        }
相關文章
相關標籤/搜索