随机生成密码的类[原创]

自己写的一个随机生成密码的类,主要用于对于MD5或其它非对称加密的数据重设密码使用.这样避免了随机生成的单一和简单的缺点.
该类可以根据需要设置生成的位数和种类.在新建实例时传入参数即可.如果要生成其它特殊的字符,可以更改GetRandomStringFromIntArray方法来实现.
现在生成的密码没有乱序,欢迎大家一起来完成这个小程序.请留言.
由于程序过于简单,也没有什么版权可言.只是大家在使用过程中,发现更好的思路或建议,请告诉我:sxlfybb@163.com



using
System;

 

namespace WyRandom

{

     /// <summary>

     /// WyRandom 的摘要说明。

     /// 编写时间:2005-06-25

     /// 编写 人:房客
     /// 编写目的:生成一个随机序列的密码

     /// </summary>

     ///

 

     public class Rnd

     {

 

         private int intCount = 7; //生成位数

         private int intTypeCount = 3;    //生成类型的数目

 

         public Rnd()

         {

              //

              // TODO: 在此处添加构造函数逻辑

              //

         }

 

         public Rnd( int Count )

         {

              this.intCount = Count;

         }

 

         public Rnd( int Count, int TypeCount )

         {

              this.intCount = Count;

              this.intTypeCount = TypeCount;

         }

 

         /// <summary>

         /// 根据实例化返回随机生成的字符串

         /// </summary>

         /// <returns>返回随机生成的字符串</returns>

         public string GetRandomNumber()

         {

              string strOut = GetRandomStringFromIntArray(intArray( this.intCount,this.intTypeCount ));

              return strOut;

         }

 

         /// <summary>

         /// 返回总数为生成位数的数组

         /// </summary>

         /// <param name="intCount">要生成的位数</param>

         /// <param name="intTypeCount">生成类型的数目</param>

         /// <returns>输出总数为生成位数的数组</returns>

         private int[] intArray( int intCount ,int intTypeCount )

         {

              int[] intArr = new int[intTypeCount];

              int intTemp = 0;

              int intTempNumber = 0;

              System.Random ranNumber = new System.Random( System.DateTime.Now.Millisecond );

              for ( int i = 0; i < intTypeCount-1; i++ )

              {

                   do

                   {

                       intTempNumber = ranNumber.Next( 0,intCount );

                   }

                   while( intTemp + intTempNumber > intCount );

                   intTemp += intTempNumber;

                   intArr[i] = intTempNumber;

              }

              intArr[intTypeCount-1] = intCount-intTemp;

              return intArr;

         }

 

         /// <summary>

         /// 由序列数组生成字符串

         /// </summary>

         /// <param name="intArrayNumber">intArray生成的数据</param>

         /// <returns>返加字符串</returns>

         private string GetRandomStringFromIntArray( int[] intArrayNumber )

         {

              System.Random CharRand = new Random( System.DateTime.Now.Millisecond );

              string strTemp = "";

              for( int i=0; i<intArrayNumber.Length; i++ )

              {

                   for ( int j=0 ;j<intArrayNumber[i] ; j++)

                       {

                       if ( i == 0)

                            strTemp += Convert.ToChar(CharRand.Next(65,90)).ToString(); //A-Z

                       if ( i ==1 )

                            strTemp += Convert.ToChar(CharRand.Next(97,122)).ToString(); //a-z

                       if ( i==2 )

                            strTemp += Convert.ToChar(CharRand.Next(49,57)).ToString(); //1-9 没用"0"是为了避免和字母O混淆

                       }

              }

              return strTemp;

         }

 

     }

}

 

 

posted on 2005-06-28 17:51  房客  阅读(993)  评论(0编辑  收藏  举报

导航