Software registration based on python scripts (machine code + registration code mechanism) and python scripts
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. Thank you very much for your support for the help House website!