zoukankan      html  css  js  c++  java
  • 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;
            }
  • 相关阅读:
    .NET-记一次架构优化实战与方案-梳理篇
    .net core实践系列之SSO-跨域实现
    Vue
    C# WPF
    开源框架
    开源框架
    开源框架
    开源框架
    WCF
    WCF
  • 原文地址:https://www.cnblogs.com/xbj-hyml/p/11301400.html
Copyright © 2011-2022 走看看