How To Make A Keygen In Visual Basic
Most commercial software uses some form of random keys to authenticate and register a legitimate copy when the program is installed. Commonly this is a series of random letters grouped in varying numbers of letters. For example, you might see this sequence: DXUWB-
GPHQE-CCNYQ-QFHAT-ZFBLO on a licensed program. Specialized software called Key Generators, or KeyGen creates these codes, but you can make your own using only 16 lines of Visual Basic code.
- Start a new project in Visual Basic, selecting the “Standard.EXE” template from the list offered under “File” and “New Project.” Click on “File,” “Save Project As” and name both the form and project “MyKeyGen” when prompted.
- Add a “CommandButton” control to the open form by double clicking this control in the Toolbox on the left of your screen. This control is an icon shaped like a small rectangle. Normally it is the third control down in the right hand column of the ToolBox. Add a label to the form in the same way. The label icon is a large letter “A” in the toolbox.
- Click on the new “Command1” button now on the form to focus on its properties in the “Properties” panel on the right. Double click on the “(Name)” property to highlight the default name, “Command1.” Change this name to “KeyGen”. Click on the caption property and change it to “Generate Key”. Click on the new Label1 on the form and make these changes to its properties:
Delete the Caption name.
Scroll down in the properties list until you find “Height” and change this to 500.
Scroll to the bottom of the properties and change the “Width” to 1200.
- Click on “View” in the main Visual Basic menu at the top and chose “Code.” This opens the Code window where you should type these lines exactly as they appear:
Option Base 1
Private Sub KeyGen_Click()
Dim n As Integer
Dim KeyGen(26) As Long
Dim NewKey, FinalKey As String
For n = 1 To 26
KeyGen(n) = Int(Rnd * 26) + 1
KeyGen(n) = KeyGen(n) + 64
NewKey = Chr$(KeyGen(n))
FinalKey = FinalKey + NewKey
FinalKey = Left(FinalKey, 5) + “-” + Mid(FinalKey, 6, 5) + “-” + Mid(FinalKey, 11, 5) + “-” + Mid(FinalKey, 17, 5)
Label1.Caption = FinalKey
- Save the project by clicking on “File” and “Save Project.” Press “F5” to run the program. The declarations in the lines beginning “Dim” specify how the program variables are used. “KeyGen(26)” creates an array of variables with 26 possibilities and the “Option Base 1” ensures the first of the 26 is numbered one. “Randomize” generates a new random seed each time the program runs. The rest of the lines create a series of random numbers, which are then converted to letters. Since ASCII code for the alphabet begins with ASCII 65, we have to add 64 to each random number before the conversion. In the end, a list of four sets of random letters separated by hyphens is created and displayed in the label box.
Tips and Warnings
- If you need a longer series of key letters, add an additional segment to the end of the line beginning “Final Key = Left(Finalkey,5)”. Insert at the end of this line the following code:
- + “-” + Right(FinalKey,5)
- To create four letter sequences, or vary the number of letters in each group, change the fives in the FinalKey line to other numbers.
- Using arrays can be tricky, particularly when you set a limit on the maximum number and use repeating sequences. If you get a message “Run-time error 9, subscript out of range,” you entered a larger number in one of the lines than the array can hold.