广告
返回顶部
首页 > 资讯 > 后端开发 > ASP.NET >asp.net 生成随机密码的具体代码
  • 719
分享到

asp.net 生成随机密码的具体代码

asp.net随机密码 2022-11-15 22:11:20 719人浏览 八月长安
摘要

public static class RandomPassWord   {       // Def


public static class RandomPassWord
   {
       // Define default min and max password lengths.
       private static int DEFAULT_MIN_PASSWORD_LENGTH = 8;
       private static int DEFAULT_MAX_PASSWORD_LENGTH = 10;

       // Define supported password characters divided into groups.
       private static string PASSWORD_CHARS_LCASE = "abcdefgijkmnopqrstwxyz";
       private static string PASSWORD_CHARS_UCASE = "ABCDEFGHJKLMNPQRSTWXYZ";
       private static string PASSWORD_CHARS_NUMERIC = "23456789";
       private static string PASSWORD_CHARS_SPECIAL = "*$-+?_&=!%{}/";

       /// <summary>
       /// Generates a random password.
       /// </summary>
       /// <returns>
       /// Randomly generated password.
       /// </returns>
       /// <remarks>
       /// The length of the generated password will be determined at
       /// random. It will be no shorter than the minimum default and
       /// no longer than maximum default.
       /// </remarks>
       public static string Generate()
       {
           return Generate(DEFAULT_MIN_PASSWORD_LENGTH,
                           DEFAULT_MAX_PASSWORD_LENGTH);
       }

       /// <summary>
       /// Generates a random password of the exact length.
       /// </summary>
       /// <param name="length">
       /// Exact password length.
       /// </param>
       /// <returns>
       /// Randomly generated password.
       /// </returns>
       public static string Generate(int length)
       {
           return Generate(length, length);
       }

       /// <summary>
       /// Generates a random password.
       /// </summary>
       /// <param name="minLength">
       /// Minimum password length.
       /// </param>
       /// <param name="maxLength">
       /// Maximum password length.
       /// </param>
       /// <returns>
       /// Randomly generated password.
       /// </returns>
       /// <remarks>
       /// The length of the generated password will be determined at
       /// random and it will fall with the range determined by the
       /// function parameters.
       /// </remarks>
       public static string Generate(int minLength,
                                     int maxLength)
       {
           // Make sure that input parameters are valid.
           if (minLength <= 0 || maxLength <= 0 || minLength > maxLength)
               return null;

           // Create a local array containing supported password characters
           // grouped by types. You can remove character groups from this
           // array, but doing so will weaken the password strength.
           char[][] charGroups = new char[][]
       {
           PASSWORD_CHARS_LCASE.ToCharArray(),
           PASSWORD_CHARS_UCASE.ToCharArray(),
           PASSWORD_CHARS_NUMERIC.ToCharArray(),
           PASSWORD_CHARS_SPECIAL.ToCharArray()
       };

           // Use this array to track the number of unused characters in each
           // character group.
           int[] charsLeftInGroup = new int[charGroups.Length];

           // Initially, all characters in each group are not used.
           for (int i = 0; i < charsLeftInGroup.Length; i++)
               charsLeftInGroup[i] = charGroups[i].Length;

           // Use this array to track (iterate through) unused character groups.
           int[] leftGroupsOrder = new int[charGroups.Length];

           // Initially, all character groups are not used.
           for (int i = 0; i < leftGroupsOrder.Length; i++)
               leftGroupsOrder[i] = i;

           // Because we cannot use the default randomizer, which is based on the
           // current time (it will produce the same "random" number within a
           // second), we will use a random number generator to seed the
           // randomizer.

           // Use a 4-byte array to fill it with random bytes and convert it then
           // to an integer value.
           byte[] randomBytes = new byte[4];

           // Generate 4 random bytes.
           RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
           rng.GetBytes(randomBytes);

           // Convert 4 bytes into a 32-bit integer value.
           int seed = (randomBytes[0] & 0x7f) << 24 |
                       randomBytes[1] << 16 |
                       randomBytes[2] << 8 |
                       randomBytes[3];

           Random random = new Random(seed);
           char[] password = null;

           // Allocate appropriate memory for the password.
           if (minLength < maxLength)
               password = new char[random.Next(minLength, maxLength + 1)];
           else
               password = new char[minLength];

           // Index of the next character to be added to password.
           int nextCharIdx;

           // Index of the next character group to be processed.
           int nextGroupIdx;

           // Index which will be used to track not processed character groups.
           int nextLeftGroupsOrderIdx;

           // Index of the last non-processed character in a group.
           int lastCharIdx;

           // Index of the last non-processed group.
           int lastLeftGroupsOrderIdx = leftGroupsOrder.Length - 1;

           // Generate password characters one at a time.
           for (int i = 0; i < password.Length; i++)
           {
               // If only one character group remained unprocessed, process it;
               // otherwise, pick a random character group from the unprocessed
               // group list. To allow a special character to appear in the
               // first position, increment the second parameter of the Next
               // function call by one, i.e. lastLeftGroupsOrderIdx + 1.
               if (lastLeftGroupsOrderIdx == 0)
                   nextLeftGroupsOrderIdx = 0;
               else
                   nextLeftGroupsOrderIdx = random.Next(0,
                                                        lastLeftGroupsOrderIdx);

               // Get the actual index of the character group, from which we will
               // pick the next character.
               nextGroupIdx = leftGroupsOrder[nextLeftGroupsOrderIdx];

               // Get the index of the last unprocessed characters in this group.
               lastCharIdx = charsLeftInGroup[nextGroupIdx] - 1;

               // If only one unprocessed character is left, pick it; otherwise,
               // get a random character from the unused character list.
               if (lastCharIdx == 0)
                   nextCharIdx = 0;
               else
                   nextCharIdx = random.Next(0, lastCharIdx + 1);

               // Add this character to the password.
               password[i] = charGroups[nextGroupIdx][nextCharIdx];

               // If we processed the last character in this group, start over.
               if (lastCharIdx == 0)
                   charsLeftInGroup[nextGroupIdx] =
                                             charGroups[nextGroupIdx].Length;
               // There are more unprocessed characters left.
               else
               {
                   // Swap processed character with the last unprocessed character
                   // so that we don't pick it until we process all characters in
                   // this group.
                   if (lastCharIdx != nextCharIdx)
                   {
                       char temp = charGroups[nextGroupIdx][lastCharIdx];
                       charGroups[nextGroupIdx][lastCharIdx] =
                                   charGroups[nextGroupIdx][nextCharIdx];
                       charGroups[nextGroupIdx][nextCharIdx] = temp;
                   }
                   // Decrement the number of unprocessed characters in
                   // this group.
                   charsLeftInGroup[nextGroupIdx]--;
               }

               // If we processed the last group, start all over.
               if (lastLeftGroupsOrderIdx == 0)
                   lastLeftGroupsOrderIdx = leftGroupsOrder.Length - 1;
               // There are more unprocessed groups left.
               else
               {
                   // Swap processed group with the last unprocessed group
                   // so that we don't pick it until we process all groups.
                   if (lastLeftGroupsOrderIdx != nextLeftGroupsOrderIdx)
                   {
                       int temp = leftGroupsOrder[lastLeftGroupsOrderIdx];
                       leftGroupsOrder[lastLeftGroupsOrderIdx] =
                                   leftGroupsOrder[nextLeftGroupsOrderIdx];
                       leftGroupsOrder[nextLeftGroupsOrderIdx] = temp;
                   }
                   // Decrement the number of unprocessed groups.
                   lastLeftGroupsOrderIdx--;
               }
           }

           // Convert password characters into a string and return the result.
           return new string(password);
       }
   }

--结束END--

本文标题: asp.net 生成随机密码的具体代码

本文链接: https://www.lsjlt.com/news/172455.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • asp.net 生成随机密码的具体代码
    复制代码 代码如下:public static class RandomPassword   {     &nb...
    99+
    2022-11-15
    asp.net 随机密码
  • vue3生成随机密码的示例代码
    目录实现效果实现思路完成布局完成生成随机数的方法完成生成随机密码的方法部分代码总结实现效果 实现思路 完成布局完成生成随机数的方法完成生成随机密码的方法 完成布局 布局直接用ele...
    99+
    2022-11-13
  • Python随机生成密码
    废话不说,直贴代码 # coding:utf-8 """ Author : han Email : oaixnah@163.com Time : 2019-07-27 17:1...
    99+
    2023-01-31
    密码 Python
  • nodejs密码加密中生成随机数的实例代码
    之前关于写了一个 nodejs密码加密中生成随机数,最近需要回顾,就顺便发到随笔上了 方法一: Math.random().toString(36).substr(2)运行后的结果就是11位数的随...
    99+
    2022-06-04
    随机数 实例 密码
  • python生成随机密码串
       今天修改服务器密码,想来想去不知道设置什么密码比较好,索性设置随机数吧。python当中的random模块可以生成随机数,主要用这个生成随机密码。    顺便在讲一下string模块中的3个函数:string.letters,str...
    99+
    2023-01-31
    密码 python
  • python怎么生成随机密码
    Python中可以使用`random`模块来生成随机密码。下面是一个生成随机密码的示例代码:```pythonimport rand...
    99+
    2023-08-25
    python
  • linux随机密码生成工具mkpasswd的示例分析
    这篇文章主要为大家展示了“linux随机密码生成工具mkpasswd的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“linux随机密码生成工具mkpasswd的示例分析”这篇文章吧。li...
    99+
    2023-06-09
  • python实例--随机密码生成器
          最近在学习python,抽空看了看图形化模块wx,顺手写了个随机密码生成器,程序运行界面如下图:(注:在Ubuntu下运行结果)   源代码如下:   import wx import string import random...
    99+
    2023-01-31
    生成器 实例 密码
  • Linux中怎么生成随机密码
    Linux中怎么生成随机密码,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。使用 mkpasswd 实用程序生成密码mkpasswd 在基于 RHEL 的系统上随 expect ...
    99+
    2023-06-16
  • Linux怎样随机生成密码mkpasswd
    本篇文章给大家分享的是有关Linux怎样随机生成密码mkpasswd,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。mkpasswd是个小工具,可以随机生产密码,用起来很方便,默...
    99+
    2023-06-06
  • linux 随机密码生成工具mkpasswd详解及实例
    linux 随机密码生成工具mkpasswd详解及实例 mkpasswd命令生成随机复杂密码,前提安装expect,然后执行mkpasswd命令即可生成随机的密码。 一、基本的命令安装 安装expect:...
    99+
    2022-06-04
    详解 实例 密码
  • Shell脚本怎样生成随机密码
    这篇文章主要为大家展示了“Shell脚本怎样生成随机密码”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Shell脚本怎样生成随机密码”这篇文章吧。生成随机密码(urandom版本)  #!/bi...
    99+
    2023-06-05
  • Shell脚本如何生成随机密码
    这篇文章主要介绍Shell脚本如何生成随机密码,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!1.生成随机密码(urandom版本)#!/bin/bash#Author:丁丁历险(Jacob)#/dev/urandom...
    99+
    2023-06-09
  • 利用Python如何生成随机密码
    本位实例为大家分享了Python生成随机密码的实现过程,供大家参考,具体内容如下 写了个程序,主要是用来检测MySQL数据库的空密码和弱密码的, 在这里,定义了三类弱密码: 1. 连续数字,譬如123456...
    99+
    2022-06-04
    密码 Python
  • 怎么使用vue3生成随机密码
    这篇文章主要介绍“怎么使用vue3生成随机密码”,在日常操作中,相信很多人在怎么使用vue3生成随机密码问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么使用vue3生成随机密码”的疑惑有所帮助!接下来,请跟...
    99+
    2023-07-02
  • Python随机生成带特殊字符的密码
    在日常运维中,如果涉及到用户管理,就一定会用到给用户设置密码的工作,其实吧,平时脑子里觉得设置个密码没什么,但要真让你随手敲一个12位带特殊字符的随机密码,也是很痛苦的事,如果让你敲10个这样的随机密码,我...
    99+
    2022-06-04
    特殊字符 密码 Python
  • 使用python3来生成安全的随机密码
    最近1年自学了python,发现python的应用场景挺多,自己百度了加自己稍微修改,写了段可以随时生成指定长度的安全随机密码#C:\Python36#coding=utf-8import stringfrom random import ...
    99+
    2023-01-31
    密码
  • JavaScript生成随机数的代码怎么写
    今天小编给大家分享一下JavaScript生成随机数的代码怎么写的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我...
    99+
    2022-10-19
  • JavaScript随机数生成代码怎么写
    在JavaScript中,可以使用Math.random()方法生成一个0到1之间的随机数。可以将这个随机数乘以一个范围的长度,并将...
    99+
    2023-10-12
    JavaScript
  • java随机验证码生成实现实例代码
    java随机验证码生成实现实例代码摘要: 在项目中有很多情况下都需要使用到随机验证码,这里提供一个java的随机验证码生成方案,可以指定难度,生成的验证码可以很方便的和其他组件搭配之前要使用一个生成随机验证码的功能,在网上找了一下,有很多的...
    99+
    2023-05-31
    java 随机 验证码
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作