Program encryption method of VB

Source: Internet
Author: User
Tags date exit goto ini
The advantages of VB I will not say more. VB Beginner imitation ability is very strong, always hope that their program looks professional, such as password login, production limit version, limited edition, forced to start and so on, in fact, these things are not mysterious, in VB as long as a small number of code can be achieved.
1. The simplest executable file password login:
Add the following code when the program starts:
Private Sub Form_Load ()
Dim A as Variant
A=inputbox ("Please enter password!")
If a<> "* * * * * * * Then MsgBox password error, you can not use the software! ": End NOTE: * * * pre-set character
End Sub
How, can frighten people. What the? Too bad!
2. Set up a file on the hard disk to store the password, so that you can read and write modified
On Error GoTo SSS
Note: If the file does not exist, catch the error and establish a password
Open ("C:\abc.abc") for Input as #
Note: File exists, open file
Input #1,b Note: Read the password into variable b
Close #
A=inputbox ("Please enter password!")
If a<>b Then MsgBox "Password is wrong, you cannot use the software!" ": End
Exit Sub
Sss:
A=inputbox ("Please build a password!")
Open ("C:\abc.abc") for Output as a comment: Create a password-storing file on your hard disk
Print #2,a
Close to the
MsgBox "Build the password successfully!"
The disadvantage of using InputBox to enter a password is that the password is displayed, you can build another form instead of the Input dialog box, add a TextBox and set its PasswordChar property to *. File Abc.abc can open edits with any text file, so before you learn to encrypt algorithms, name the file *.sys or *.dll, and place it under Windows or the system directory, and even set its properties to hidden, haha, system files who dare to change! However, be careful not to overwrite the real system files.
Using the method of starting a login to encrypt the user will be disgusted, preferably only in the software more important to modify the data section or for multiple user login. If you want to make shareware, try a limited edition first.
3. Software limit the number of times it is also a place in the hard disk to make a mark, every start to count once, when the number of added to a certain value does not allow the use of software. The above code can also be implemented with a slight change:
Private Sub Form_Load ()
On Error GoTo SSS
Note: If the file does not exist, create the file
Open ("C:\abc.abc") for Input as #
Note: File exists, open file
Input #1,b Note: Read the value into variable b
Close #
If b>100 Then MsgBox "Sorry, you can only use the Software 100 times!" ": End
Note: Prompts the user to use the number and exits the program
C=B+1 Note: Counter plus 1
Open ("C:\abc.abc") for Output as #3
Print #3,c Note: The value added after 1 is written to the file
Close #3
Exit Sub
Sss:
Open ("C:\abc.abc") for Output as
Print #2,1 Note: Create a file and write a value of 1
Close to the
End Sub
4. You must be familiar with the Win.ini and System.ini files, which is a text file specifically designed to hold application initialization information and run environment information, and the acquisition and preservation of initialization parameters for Windows software is achieved by reading a text file with an. ini extension. At present, many software simply save the software password in their INI file. VB as long as the use of API GetPrivateProfileString and writeprivateprofilestring two functions can easily read and write INI file, so you can save, read out and verify the password. First know the INI file.
INI file in the form of:
[Section1]
Keyword1=value1
Keyword2=value2
......
[Section2]
Keyword1=value1
Keyword2=value2
......
section is the name of the segment, keyword is the keyword name, and value is the corresponding setting for the keyword.
First create a new segment name and keyword name with writeprivateprofilesection:
Declare Function writeprivateprofilesection Lib "kernel32" Alias "Writeprivateprofilesectiona" (ByVal Lpappname as String,byval lpstring As String, ByVal lpFileName as String) as Long
Declare Function writeprivateprofilestring Lib "kernel32" Alias "Writeprivateprofilestringa" (ByVal lpapplicationname As String,byval Lpkeyname as any, ByVal lpstring as any, ByVal lpFileName as String) as Long
A=writeprivateprofilesection ("User", "password", "C:\windows\user.ini")
Create the new segment name user and the keyword password in the User.ini file in the Windows directory, and create the file if there is no User.ini file under the directory
B=writeprivateprofilestring ("User", "password", "1234", "C:\windows\user.ini"), set key user value of 1234. This will give you an extra paragraph in your User.ini file:
[User]
password=1234
The value of password can be read by using the GetPrivateProfileString function:
Declare Function getprivateprofilestring Lib "kernel32" Alias "Getprivateprofilestringa" (ByVal Lpapplicationname as String,byval Lpkeyname as Any,byval Lpdefault as String,byval lpreturnedstring as String,byval nSize as Long,ByVal Ame as String) as Long
Dim Key as string*255
C=getprivateprofilestring ("User", "password", "false", key,255, "C:\windows\user.ini")
If key= "false" then
MsgBox "file does not exist or does not have this field"
Else:Form1.Print "The password is"; key
This function assigns the value of the password in the file User.ini (that is, your password) to key, and if an error occurs (the file does not exist or does not have the name) the value of the key is "false", note that the length of the key must be declared and consistent with the value in the function. This allows you to determine whether to continue running the program by contrasting the key with the login password or by handling the value of the key directly.
The advantage of using the INI file to store passwords is that designers can create several segment names to store different passwords, enabling multiple user logins.
5. Marking passwords in the registry may be the highest level of protection for your work. The main method is to create a key in the registry, in the key value of your password, the next run time to take out the data for verification or processing, when the conditions to terminate the program. You can think that the registry is "to win", as long as you choose to do a hidden location to mark or store data, do not have to do any encryption algorithm processing should be relatively safe. It is surprising that VB can easily use the API to manipulate the registry. Here is a brief introduction to a few API functions, as long as the reference function description, correct reference variables to pass data, do not need any skills to operate the registry.
RegCreateKeyEx: Creates a keyword that, if the keyword already exists, will simply open it
RegOpenKey: For opening a key
RegSetValueEx: When a key is opened, it is used to set its key value
RegQueryValueEx: Query for a value that exists, and return the ERROR_SUCCESS flag if the function call succeeds
Make a limited edition as long as you can use several functions such as day, month, year, date on the line. For example, the procedure cannot be carried out in 2001 years:
A=year (Date)
If a>=2001 then MsgBox "Sorry, the software has expired": End
You can also use the previous method to make a mark on the hard disk when the condition is met, and the user can no longer use the software by modifying the system time and reinstalling.
What do you think? Learned the hard disk's simple read and write operation, these dongdong is not a mystery! Although not very clever, but many software do use this method for simple encryption; As the decryption means more and more sophisticated, the single encryption method has become the past, some software at the same time in the INI file and registry, such as marking, of course, is not simply to save your input, Win9x dial-up Internet If you choose to save the password will also be generated on the hard disk USER.PWL file, but the file is added to the encryption, forced to open with a text editor will only see some garbled. Haha, again heart itch want to learn other tricks!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.