My Submission (No Labels )+Comments (Click to View)
This could very easily be expanded on to make a decent Captcha verifier class. I whipped this up before bed so I didn't put any effort into a GUI. I mainly wanted to show a different way to do this rather then generating a Label.
Enjoy.
Code:
Imports System.Drawing
Imports System.Security.Cryptography
Public Class Form1
Dim x As String 'used to store the text before painting, then stores the MD5 of what was painted
Private Sub btnGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
'new bitmap to draw the captcha
Dim xBitmap As New Bitmap(200, 50)
'new brush to paint with
Dim xBrush As New SolidBrush(Color.Black)
'font used to draw captcha
Dim xFont As New Font(FontFamily.GenericSansSerif, 20)
'create a new graphic from the bitmat we created
Dim gfx As Graphics = Graphics.FromImage(xBitmap)
'return an 8 character string
x = NewRand()
'draw our string to the graphic
gfx.DrawString(x, xFont, xBrush, 0, 0)
'set the picture box image to our captcha
pbCap.Image = xBitmap
'md5 our captcha so incase stolen(via windows handle)
x = TxtHash(x)
End Sub
Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click
'hash the answer and verify against the hashed captcha
If TxtHash(txtAnswer.Text) = x Then
MessageBox.Show("Sweet balls you got it!")
Else
MessageBox.Show("Uh oh, you suck.")
End If
End Sub
Private Function NewRand()
'characters allowed in the captcha
Dim strText As String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
'new random number generator()
Dim rand As New Random
'holds our random character string
Dim xString As String = ""
For i As Integer = 0 To 8
'loops 8 times, getting a random character from our allowed characters
xString += strText.Substring(rand.Next(strText.Length - 1), 1)
Next
'return the result
Return xString
End Function
Private Function TxtHash(ByVal str As String)
'new md5 class
Dim hash As MD5 = MD5.Create()
'md5 hash in byte form
Dim bytes As Byte() = hash.ComputeHash(System.Text.Encoding.ASCII.GetBytes(str))
'return the md5 as a string
Return BitConverter.ToString(bytes).Replace("-", "")
End Function
End Class
I code at http://tech.reboot.pro