using System;
using System.Collections.Generic;
using System.Text;

/*说明: 生成一个随机的密码,包括纯数字,纯字母,字母和数字,
*作者: Zoonware
*时间: 2009年8月10日
*版本: 1.0
*/

public class RandomPassword
{
    /// <summary>
    /// 生成各类随机密码,包括纯字母,纯数字,带特殊字符等,除非字母大写密码类型,其余方式都将采用小写密码
    /// </summary>
    /// <param name="pwdType">密码类型 大写为"UPPER",小写为"LOWER",数字为"NUMBER",字母与数字为"NUMCHAR",数字字母字符都包括为"ALL" </param>
    /// <param name="length">密码长度,最小为6位</param>
    /// <returns></returns>
    public 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;
    }
}

  转自 http://hi.baidu.com/sunny_q

posted on 2012-06-10 21:00  小白白小  阅读(224)  评论(0)    收藏  举报