需求:產生驗證碼、臨時密碼、唯一碼等。本函數 GetRndCode(maxLen) 以 Randomize 初始化隨機種子,迴圈從大寫字母 (65-90) 與數字 (48-57) 兩組 ASCII 範圍隨機挑選,組合成指定長度字串。
Public Function GetRndCode(maxLen)
Dim strNewPass, whatsNext, upper, lower, intCounter
Randomize
For intCounter = 1 To maxLen
' 0=字母, 1=數字
whatsNext = Int((1 - 0 + 1) * Rnd + 0)
If whatsNext = 0 Then
upper = 90 ' 'Z'
lower = 65 ' 'A'
Else
upper = 57 ' '9'
lower = 48 ' '0'
End If
strNewPass = strNewPass & Chr(Int((upper - lower + 1) * Rnd + lower))
Next
GetRndCode = strNewPass
End Function
Dim code
code = GetRndCode(15) ' 產生 15 碼隨機字串
Response.Write code ' 範例輸出: K7B9M2P4Q8R1T3V
!@#$%) 強化密碼複雜度。RNGCryptoServiceProvider。GetRndCode(4) 取得 4 碼字串存入 Session("vcode")。verification.asp 使用 Server.CreateObject("WIA.ImageFile") 或第三方元件 (ASPJpeg) 繪製干擾線、噪點、文字。Response.ContentType = "image/jpeg" 輸出二進位圖片。<img src="verification.asp"> 顯示。