05-07-2011, 10:43 AM
Here's my submission then...
Module1 - Generate Captcha
Form1 - Captcha Verification
Download .EXE: http://localhostr.com/file/6O0KRds/Captcha.exe
Spoiler (Click to View)
Module1 - Generate Captcha
Code:
Imports System
Imports System.Security.Cryptography
Module Module1
Public Class RandomCAPTCHA
Private Shared DEFAULT_MIN_CAPTCHA_LENGTH As Integer = 1
Private Shared DEFAULT_MAX_CAPTCHA_LENGTH As Integer = 1
Private Shared CAPTCHA_CHARS_UCASE As String = "ABCDEFGHJKLMNPQRSTWXYZ"
Private Shared CAPTCHA_CHARS_LCASE As String = "abcdefgijkmnopqrstwxyz"
Private Shared CAPTCHA_CHARS_NUMERIC As String = "123456789"
Private Shared CAPTCHA_CHARS_SPECIAL As String = "*$-+?_&=!%{}/"
Public Shared Function Generate() As String
Generate = Generate(DEFAULT_MIN_CAPTCHA_LENGTH, DEFAULT_MAX_CAPTCHA_LENGTH)
End Function
Public Shared Function Generate(ByVal length As Integer) As String
Generate = Generate(length, length)
End Function
Public Shared Function Generate(ByVal minLength As Integer, ByVal maxLength As Integer) As String
If (minLength <= 0 Or maxLength <= 0 Or minLength > maxLength) Then
Generate = Nothing
End If
Dim charGroups As Char()() = New Char()() {CAPTCHA_CHARS_LCASE.ToCharArray(), CAPTCHA_CHARS_UCASE.ToCharArray(), CAPTCHA_CHARS_NUMERIC.ToCharArray(), CAPTCHA_CHARS_SPECIAL.ToCharArray()}
Dim charsLeftInGroup As Integer() = New Integer(charGroups.Length - 1) {}
Dim I As Integer
For I = 0 To charsLeftInGroup.Length - 1
charsLeftInGroup(I) = charGroups(I).Length
Next
Dim leftGroupsOrder As Integer() = New Integer(charGroups.Length - 1) {}
For I = 0 To leftGroupsOrder.Length - 1
leftGroupsOrder(I) = I
Next
Dim randomBytes As Byte() = New Byte(3) {}
Dim rng As RNGCryptoServiceProvider = New RNGCryptoServiceProvider()
rng.GetBytes(randomBytes)
Dim seed As Integer = ((randomBytes(0) And &H7F) << 24 Or randomBytes(1) << 16 Or randomBytes(2) << 8 Or randomBytes(3))
Dim random As Random = New Random(seed)
Dim CAPTCHA As Char() = Nothing
If (minLength < maxLength) Then
CAPTCHA = New Char(random.Next(minLength - 1, maxLength)) {}
Else
CAPTCHA = New Char(minLength - 1) {}
End If
Dim nextCharIdx As Integer
Dim nextGroupIdx As Integer
Dim nextLeftGroupsOrderIdx As Integer
Dim lastCharIdx As Integer
Dim lastLeftGroupsOrderIdx As Integer = leftGroupsOrder.Length - 1
For I = 0 To CAPTCHA.Length - 1
If (lastLeftGroupsOrderIdx = 0) Then
nextLeftGroupsOrderIdx = 0
Else
nextLeftGroupsOrderIdx = random.Next(0, lastLeftGroupsOrderIdx)
End If
nextGroupIdx = leftGroupsOrder(nextLeftGroupsOrderIdx)
lastCharIdx = charsLeftInGroup(nextGroupIdx) - 1
If (lastCharIdx = 0) Then
nextCharIdx = 0
Else
nextCharIdx = random.Next(0, lastCharIdx + 1)
End If
CAPTCHA(I) = charGroups(nextGroupIdx)(nextCharIdx)
If (lastCharIdx = 0) Then
charsLeftInGroup(nextGroupIdx) = _
charGroups(nextGroupIdx).Length
Else
If (lastCharIdx <> nextCharIdx) Then
Dim temp As Char = charGroups(nextGroupIdx)(lastCharIdx)
charGroups(nextGroupIdx)(lastCharIdx) = _
charGroups(nextGroupIdx)(nextCharIdx)
charGroups(nextGroupIdx)(nextCharIdx) = temp
End If
charsLeftInGroup(nextGroupIdx) = _
charsLeftInGroup(nextGroupIdx) - 1
End If
If (lastLeftGroupsOrderIdx = 0) Then
lastLeftGroupsOrderIdx = leftGroupsOrder.Length - 1
Else
If (lastLeftGroupsOrderIdx <> nextLeftGroupsOrderIdx) Then
Dim temp As Integer = _
leftGroupsOrder(lastLeftGroupsOrderIdx)
leftGroupsOrder(lastLeftGroupsOrderIdx) = _
leftGroupsOrder(nextLeftGroupsOrderIdx)
leftGroupsOrder(nextLeftGroupsOrderIdx) = temp
End If
lastLeftGroupsOrderIdx = lastLeftGroupsOrderIdx - 1
End If
Next
Generate = New String(CAPTCHA)
End Function
End Class
Sub Main()
Dim I As Integer
For I = 1 To 1
Form1.Label1.Text = RandomCAPTCHA.Generate(1)
Form1.Label2.Text = RandomCAPTCHA.Generate(1)
Form1.Label3.Text = RandomCAPTCHA.Generate(1)
Form1.Label4.Text = RandomCAPTCHA.Generate(1)
Form1.Label5.Text = RandomCAPTCHA.Generate(1)
Form1.Label6.Text = RandomCAPTCHA.Generate(1)
Next
End Sub
End Module
Form1 - Captcha Verification
Code:
Public Class Form1
Private Sub Generate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Generate.Click
Call Main()
Verification.Clear()
End Sub
Private Sub Test_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Test.Click
Dim aa = Label1.Text
Dim bb = Label2.Text
Dim cc = Label3.Text
Dim dd = Label4.Text
Dim ee = Label5.Text
Dim ff = Label6.Text
If Verification.Text = aa + bb + cc + dd + ee + ff Then
MsgBox("You did it!")
Else
MsgBox("Nop!")
End If
End Sub
End Class
Download .EXE: http://localhostr.com/file/6O0KRds/Captcha.exe