純數字驗證碼的實現相對比較簡單,可通過以下兩種方法來實現
(1)使用隨機數方式,代碼如下:
private String GetRandomint(int codeCount)
{
Random random = new Random();
string min = "";
string max = "";
for (int i = 0; i < codeCount; i++)
{
min +="1";
max+="9";
}
return (random.Next(Convert.ToInt32(min),Convert.ToInt32(max)).ToString());
}
(2)使用隨機組合方式,代碼如下:
{
string allChar = "0,1,2,3,4,5,6,7,8,9";
string[] allCharArray = allChar.Split(',');
string randomCode = "";
int temp = -1;
Random rand = new Random();
for (int i = 0; i < codeCount; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
}
int t = rand.Next(9);
if (temp == t)
{
return CreateRandomCode(codeCount);
}
temp = t;
randomCode += allCharArray[t];
}
return randomCode;
}
2、數字+字母驗證碼的實現,代碼如下:
{
string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
string[] allCharArray = allChar.Split(',');
string randomCode = "";
int temp = -1;
Random rand = new Random();
for (int i = 0; i < codeCount; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
}
int t = rand.Next(61);
if (temp == t)
{
return CreateRandomCode(codeCount);
}
temp = t;
randomCode += allCharArray[t];
}
return randomCode;
}
3、漢字驗證碼的實現,代碼如下:
/*
此函數在漢字編碼範圍內隨機創建含兩個元素的十六進制字節數組,每個字節數組代表一個漢字,並將
四個字節數組存儲在object數組中。
參數:strlength,代表需要產生的漢字個數
*/
public static object[] CreateRegionCode(int strlength)
{
//定義一個字符串數組儲存漢字編碼的組成元素
string[] rBase = new String[16] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
Random rnd = new Random();
//定義一個object數組用來
object[] bytes = new object[strlength];
/**/
/*每循環一次產生一個含兩個元素的十六進制字節數組,並將其放入bject數組中
每個漢字有四個區位碼組成
區位碼第1位和區位碼第2位作爲字節數組第一個元素
區位碼第3位和區位碼第4位作爲字節數組第二個元素
*/
for (int i = 0; i < strlength; i++)
{
//區位碼第1位
int r1 = rnd.Next(11, 14);
string str_r1 = rBase[r1].Trim();
//區位碼第2位
rnd = new Random(r1 * unchecked((int)DateTime.Now.Ticks) + i);//更換隨機數發生器的種子避免產生重複值
int r2;
if (r1 == 13)
{
r2 = rnd.Next(0, 7);
}
else
{
r2 = rnd.Next(0, 16);
}
string str_r2 = rBase[r2].Trim();
//區位碼第3位
rnd = new Random(r2 * unchecked((int)DateTime.Now.Ticks) + i);
int r3 = rnd.Next(10, 16);
string str_r3 = rBase[r3].Trim();
//區位碼第4位
rnd = new Random(r3 * unchecked((int)DateTime.Now.Ticks) + i);
int r4;
if (r3 == 10)
{
r4 = rnd.Next(1, 16);
}
else if (r3 == 15)
{
r4 = rnd.Next(0, 15);
}
else
{
r4 = rnd.Next(0, 16);
}
string str_r4 = rBase[r4].Trim();
//定義兩個字節變量存儲產生的隨機漢字區位碼
byte byte1 = Convert.ToByte(str_r1 + str_r2, 16);
byte byte2 = Convert.ToByte(str_r3 + str_r4, 16);
//將兩個字節變量存儲在字節數組中
byte[] str_r = new byte[] { byte1, byte2 };
//將產生的一個漢字的字節數組放入object數組中
bytes.SetValue(str_r, i);
}
return bytes;
}