用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c#代码库

复杂密码生成工具

2016-08-14 作者: 小章举报

[c#]代码库

using Petr.Felzmann;
using System;
using System.IO;
using System.Xml;

namespace PasswordGenerator
{
    class Program
    {
        public const string HelpParameter = "/?";
        public const string MinParameter = "/min";
        public const string MaxParameter = "/max";
        public const string CatParameter = "/cat";
        public const string CountParameter = "/count";
        public const int CountDefault = 1;

        [STAThread]
        static void Main(string[] args)
        {
            XmlDocument categories = null;
            int minPasswordLength = -1;
            int maxPasswordLength = -1;
            int count = CountDefault;
            if (ParseArguments(
                args,
                ref categories,
                ref minPasswordLength,
                ref maxPasswordLength,
                ref count))
            {
                StrongPasswordGenerator gen = null;
                if (categories == null)
                {
                    gen = new StrongPasswordGenerator();
                }
                else
                {
                    gen = new StrongPasswordGenerator(categories);
                }

                for (int i = 0; i < count; i++)
                {
                    string password = gen.Generate(minPasswordLength, maxPasswordLength);
                    Console.WriteLine(password);
                }
            }
        }

        public static bool ParseArguments(
            string[] args,
            ref XmlDocument categories,
            ref int minPasswordLength,
            ref int maxPasswordLength,
            ref int count)
        {
            string min = null;
            string max = null;

            foreach (string arg in args)
            {
                if (arg == HelpParameter)
                {
                    Help(null);
                    return false;
                }

                string[] keyValue = arg.Split(':');

                if (keyValue.Length != 2)
                {
                    Help("The parameter [" + arg + "] has no value.");
                    return false;
                }

                string key = keyValue[0];
                string val = keyValue[1];

                switch (key)
                {
                    case MinParameter:
                        min = val;
                        break;
                    case MaxParameter:
                        max = val;
                        break;
                    case CatParameter:
                        if (File.Exists(val))
                        {
                            categories = new XmlDocument();
                            categories.Load(val);
                        }
                        else
                        {
                            Help("The file [" + val + "] does not exists.");
                            return false;
                        }
                        break;
                    case CountParameter:
                        count = int.Parse(val);
                        break;
                }
            }

            if (min == null || min == null)
            {
                Help("The parameters [" + MinParameter + "] and [" + MaxParameter + " must be set.]");
                return false;
            }

            minPasswordLength = int.Parse(min);
            maxPasswordLength = int.Parse(max);

            return true;
        }

        public static void Help(string message)
        {
            if (message != null)
            {
                Console.WriteLine(message);
                Console.WriteLine();
            }

            Console.WriteLine("The utility pwdgen generate strong passwords.");
            Console.WriteLine("\nUsage:");
            Console.WriteLine("\tpwdgen /min:MIN /max:MAX [/count:C] [/cat:PATH]");
            Console.WriteLine("\nwhere");
            Console.WriteLine("\tMIN  is the minimal password length");
            Console.WriteLine("\tMAX  is the maximal password length");
            Console.WriteLine("\tC    is the count of the passwords will be generated.");
            Console.WriteLine("\t     Default value is " + CountDefault + ".");
            Console.WriteLine("\tPATH is the path to the XML file");
            Console.WriteLine("\t     contains the definition of the categories of the characters");
            Console.WriteLine("\nExample:");
            Console.WriteLine("pwdgen /min:6 /max:10 /count:2 /cat:\"c:\\Program Files\\MyCategories.xml\"");
        }
    }
}

[源代码打包下载]




网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...