Program to generate ‘CAPTCHA’ (Random String) in Visual Basic(VB.NET). In this article, we will understand How to generate ‘CAPTCHA’ (Random String) in Visual Basic(VB.NET).
What is ‘CAPTCHA’?
CAPTCHA stands for Completely Automated Public Turing Test. It is a program that can tell humans from machines using some type of generated test. This will reduce the hackers, spam attackers, and fake visitors or entries from websites. A test most people can easily pass but a computer program cannot.
The term CAPTCHA was coined in 2000 by Luis von Ahn, Manuel Blum, Nicholas Hopper, and John Langford of Carnegie Mellon University. These 4 individuals are credited with creating the first CAPTCHA, which was used by Yahoo!
Characteristics of ‘CAPTCHA’
Modern text-based CAPTCHAs are designed such that they require the simultaneous use of three separate abilities—invariant recognition, segmentation, and parsing—to correctly complete the task with any consistency.
- Invariant recognition refers to the ability to recognize a large amount of variation in the shapes of letters.
- Segmentation, or the ability to separate one letter from another, is also made difficult in CAPTCHAs, as characters are crowded together with no white space in between.
- Context is also critical. The CAPTCHA must be understood holistically to correctly identify each character
Each of these problems poses a significant challenge for a computer, even in isolation. The presence of all three at the same time is what makes CAPTCHAs difficult to solve. Unlike computers, humans excel at this type of task. While segmentation and recognition are two separate processes necessary for understanding an image for a computer, they are part of the same process for a person.
Uses of ‘CAPTCHA’
CAPTCHA’s have many practical uses for security, including, but not limited to:
- To prevent “comment spam” in user blogs.
- Protecting registration at websites (prevents people from having “bots” to register many accounts for the purpose of spam).
- Protecting email addresses from “Scrapers” (A “Scraper” is a software bot that searches the Internet for email addresses in plain text).
- 4. Help prevent Dictionary Attacks.
Program to generate random string ‘CAPTCHA’ in Visual Basic (VB.NET)
Imports System.Drawing.Drawing2D Public Class Form1 Dim str As String Private Sub btnShow_Click(ByVal sender As Object,ByVal e As EventArgs) Handles btnReg.Click If txtReg.Text = str Then MsgBox("Register Success") Else MsgBox("Register Fail") End If End Sub Private Sub btnRefresh_Click(ByVal sender As Object,ByVal e As EventArgs) Handles btnRefresh.Click Dim NumCaptcha As String = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" str = "" Dim R As New Random For i As Integer = 0 To 5 str = str + NumCaptcha(R.Next(0, 60)) Next Dim b As New Bitmap(274, 71, Imaging.PixelFormat.Format32bppArgb) Dim g As Graphics = Graphics.FromImage(b) Dim Hb As New HatchBrush(HatchStyle.DottedDiamond, Color.FromArgb(255, 128, 0), Color.Black) g.DrawString(str, New Font("Curlz MT", 40, FontStyle.Strikeout, GraphicsUnit.Point), Brushes.White, 5, 5) picCaptcha.Image = b End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load Dim NumCaptcha As String = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" str = "" Dim R As New Random For i As Integer = 0 To 5 str = str + NumCaptcha(R.Next(0, 60)) Next Dim b As New Bitmap(274, 71, Imaging.PixelFormat.Format32bppArgb) Dim g As Graphics = Graphics.FromImage(b) Dim Hb As New HatchBrush(HatchStyle.DottedDiamond, Color.FromArgb(255, 128, 0), Color.Black) g.DrawString(str, New Font("Curlz MT", 40, FontStyle.Strikeout, GraphicsUnit.Point), Brushes.White, 5, 5) picCaptcha.Image = b End Sub End Class
Explanation
System.Drawing.Drawing2D is the two-dimensional and vector graphics functionality. The randomly generated captcha text stores in the str string variable. In the button, it validates txtReg text box and str variable. The randomly generated text stores in the str variable. Maximum stores 5 characters. The program converts text to the bit map images. The same code is applied in the form load and btn refresh button.
Example: In the above example we create one label to display randomly generated captcha text, a Refresh button to change captcha text, a Text box to enter captcha text for trusted users, and a Register button to verify the entered text is valid or not.
In this way, we learn how to write a program to generate ‘CAPTCHA’ (Random String) in Visual Basic(VB.NET).