Register software based on python scripts (machine code + registration code mechanism)

Source: Internet
Author: User
After the user runs the program, the user automatically checks the authentication status through the file. if the authentication is not performed, the user needs to register. This article describes how to implement the software registration function (machine code + registration code mechanism) based on python scripts. For more information, see I. preface:

Objective: to register an existing python image processing tool

Function: After a user runs a program, the user automatically checks the authentication status through the file. if the authentication is not performed, the user needs to register. During the registration process, the user sends the machine code (volume number of disk C) displayed after the program is run to the Administrator. the administrator encrypts the machine code and generates an encrypted file or string to return it to the user. Every time you start a program, when a file is registered, the program will be decoded by DES and base64, and compared with the serial number of the C disk volume obtained at the moment, if consistent, run the main program. If the decoded registration file is inconsistent with the volume serial number, you must be reminded to enter the registration code. if the decoded file is the same as the retrieved machine code, authentication is passed, generate a new registration file and enter the main program.

Libraries and components:

1. pyDes is used for encryption and decryption.

2. base64, used for secondary encryption and decryption after pyDes encryption and decryption

3. win32api, used to obtain the serial number of the C disk volume

4. package pyinstaller

Refer:

1. python des encryption

Http://www.mamicode.com/info-detail-508384.html

Http://twhiteman.netfirms.com/des.html

2. win32api. GetVolumeInformation

Http://timgolden.me.uk/pywin32-docs/win32api__GetVolumeInformation_meth.html

3. description of pyinstaller package

Http://pythonhosted.org/PyInstaller/spec-files.html#using-spec-files

II. implementation

# Coding: utf8 # register. py # function description: automatically checks the authentication status after the user runs the program. if the user has not been authenticated, the user needs to register. During the registration process, the user sends the machine code (volume number) displayed after the program runs the program to the Administrator. the administrator encrypts the program and generates an encrypted file or string to the user. # Upon each login, the software will be decoded by DES and base64 when there is a registration file or registration code. if the decoded code is the same as the retrieved machine code, the software will pass authentication and enter the main program. Import base64import win32apifrom pyDes import * # from binascii import a2b_hex # If you need to use binary encoding to save the registration code and registration file, you can use binascii to convert class register: def _ init _ (self ): self. des_Key = "BHC # @ * UM" # Keyself. des_IV = "\ x22 \ x33 \ x35 \ x81 \ xBC \ x38 \ x5A \ xE7" # Custom IV vector # obtain the serial number of the C disk volume # The advantage of using the serial number of the C disk is that the length is short, easy operation, such as 1513085707, but formatting the C disk or reinstalling the computer will affect the serial number of the C disk. # Win32api. getVolumeInformation (Volume Name, Volume Serial Number, Maximum Component Length of a file name, Sys Flags, File System Name) # return ('', 1513085707,255,654 70719, 'ntfs '), volume serial number is 1513085707.def getCVolumeSerialNumber (self): CVolumeSerialNumber = win32api. getVolumeInformation ("C: \") [1] # print chardet. detect (str (CVolumeSerialNumber) # print CVolumeSerialNumberif CVolumeSerialNu Mber: return str (CVolumeSerialNumber) # number is long type, has to be changed to str for comparing to content after. else: return 0 # encryption using DES and base64 # M2Crypto and rsa are considered, but def DesEncrypt (self, str): k = des (self. des_Key, CBC, self. des_IV, pad = None, padmode = PAD_PKCS5) EncryptStr = k. encrypt (str) # EncryptStr = binascii. unhexlify (k. encrypt (str) return base64.b64encode (EncryptStr) # Convert to ba Se64 encoding Returns # des decoding def DesDecrypt (self, str): k = des (self. des_Key, CBC, self. des_IV, pad = None, padmode = PAD_PKCS5) DecryptStr = k. decrypt (str) # DecryptStr = a2b_hex (k. decrypt (str) print DecryptStrreturn DecryptStr # obtain the registration code. after the verification is successful, the registration file def regist (self): key = raw_input ('Please input your register code: ') is generated :') # input strings similar to "12" that do not comply with base64 rules may cause exceptions, so you need to add input judgment # while keyif key: content = self. getCVolumeSerialNumber () // number Has been changed to str type after use str () # print chardet. detect (content) # print type (content) # print content # type (key_decrypted) is strkey_decrypted = str (self. desDecrypt (base64.b64decode (key) # print chardet. detect (key_decrypted) # print key_decrypted # type (key_decrypted) is strif content! = 0 and key_decrypted! = 0: if content! = Key_decrypted: print "wrong register code, please check and input your register code again:" self. regist () elif content = key_decrypted: print "register succeed. "# read/write files must be judged with open ('. /register ', 'w') as f: f. write (key) f. close () return Trueelse: return Falseelse: self. regist () return Falsedef checkAuthored (self): content = self. getCVolumeSerialNumber () checkAuthoredResult = 0 # to read and write files, add try: f = open ('. /register ', 'r') if f: key = f. read () if key: key_decrypted = self. desDecrypt (decrypt (key) if key_decrypted: if key_decrypted = content: checkAuthoredResult = 1 else: checkAuthoredResult =-1 else: checkAuthoredResult =-2 else: checkAuthoredResult =-3 else: self. regist () handle T IOError: print IOErrorprint checkAuthoredResultreturn checkAuthoredResultif _ name _ = '_ main _': reg = register () reg. regist ()

III. remarks

1. the reason for using the serial number of the volume C instead of the hard disk number is: the number of digits is short, which is convenient for operation.

However, using a hard disk number is safer, because the hard disk number will not be changed due to system reinstallation, disk C formatting, or disk c number modification.

#CVolumeSerialNumber: 1513085707#after encryption: ro5RVXZoP0KmnogYDeepUg==#the HardDiskNumber: 32535332584e4741343536393237204620202020#after encryption: MzI1MzUzMzI1ODRlNDc0MTM0MzUzNjM5MzIzNzIwNDYyMDIwMjAyMA==

2. in addition to win32api, wmi can also be used to obtain system information (such as the hard disk number). The process of obtaining the complete hard disk number is as follows:

# Although wmi can be used to obtain the disk serial number, the disk serial number is 3253533258 **************** 3237204620202020, which is too long after encryption and inconvenient to operate, so discard import wmidef getHardDiskNumber (self): c = wmi. WMI () for physical_disk in c. win32_DiskDrive (): return physical_disk.SerialNumber

Https://pypi.python.org/pypi/WMI/

3. chardet can be used to check the encoding type of a string. it can be used to detect equal strings.

Chardet. detect (str)

4. there are still some logical vulnerabilities, such as checking whether the file exists during file read/write and selecting the reading method.

5. register. py is used by the main function or other functions that need to obtain the authentication status.

The process of using the register class in the main function is:

Create a logIn function to obtain the authentication result. if the authentication result is false, call the regist function of the register class again to remind the user to enter the registration code, create a new registration file only when the registration code is entered successfully-if the authentication result is true, start the main program directly.

6. the administrator should also have an encryption. py, which is used to encrypt the serial number of the C disk volume sent by the user using the des + base64 algorithm. after encryption, a string or registration file is generated and then returned to the user.

The above section describes how to implement the software registration function (machine code + registration code mechanism) based on python scripts. I hope it will help you. if you have any questions, please leave a message, the editor will reply to you in a timely manner. I would like to thank you for your support for the script home website!

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.