日期:2014-05-18 浏览次数:20876 次
namespace test { class Program { static void Main(string[] args) { //测试产生的验证码 Program checking= new Program(); for (int i = 0; i < 3; i++) { string str= checking.run(4); Console.WriteLine(asd); } Console.Read(); } /// <summary> /// 生成验证码 /// </summary> /// <param name="checkingLength">产生验证码的长度</param> /// <returns>最后生成的验证码</returns> public string run(int checkingLength ) { //出现的随机数 string str = "1,2,3,4,5,6,7,8,9,0,A,a,B,b,C,c,D,d,E,e,F,f,G,g,H,h,I,i,J,j,K,k,L,l,M,m,N,n,O,o,P,p,Q,q,R,r,S,s,T,t,U,u,V,v,W,w,X,x,Y,y,Z,z"; //最后返回的验证码 string checkng = ""; Random ran = new Random(); //接收随机码的数组 string[] stringArry = str.Split(','); //随机数组的下标值 int amout = 0; for (int i = 0; i < checkingLength; i++) { //产生验证码数组的下标 int num = ran.Next(62); //判断数组下标是否相同 if (num == amout) { //下标值改变 amout += i; //果如改变的下标值超过数组索引界限 if (amout > stringArry.Length) //将其数组下标缩小 amout = amout - (num / 2); } else { amout = num; } //拼接产生的验证码 checkng += stringArry[amout]; } return checkng; } } }