Implement software registration function based on python script (machine code + registration code mechanism)

  • 2020-05-12 02:50:08
  • OfStack

1. Introduction:

Purpose: to complete the registration function of the existing python image processing tool

Function: after the user runs the program, the authentication status will be automatically detected through the file. If the user is not authenticated, he/she needs to register. The registration process is that the user sends the machine code (volume sequence number of C disk) displayed after the program runs back to the administrator, who encrypts the machine code and generates an encrypted file or a string to return to the user. Each time the program is started, the program will be decoded by DES and base64 in the case of the registration file, and will be compared with the C volume serial number obtained at the moment. If 1 is sent, the main program will be run. If the registration file is decoded and volume number is not 1, it is necessary to remind the user to enter the registration code, if the new input decoded and re-acquired machine code 1, then through authentication, generate a new registration file into the main program.

Libraries and components:

1. pyDes is used for encryption and decryption

2. base64 is used for encryption and decryption of pyDes for two times after encryption and decryption

3. win32api is used to obtain the serial number of C coil

4. pyinstaller, package


1. pyDes library realizes des encryption of python

2, win32api GetVolumeInformation

3. Package file description of pyinstaller

2. Implement

# Function description: after the user runs the program, it will automatically detect the status of authentication. If it is not authenticated, it needs to register. The registration process is that the user sends the machine code (volume sequence number) displayed after the program runs back to the administrator, who generates the encrypted file or string back to the user after encryption. 
# Every time you log in, the software passes if you have a registration file or a registration code DES and base64 Decode if decoded after and reacquire the machine code 1 To, through the authentication, into the main program. 
import base64
import win32api
from pyDes import *
#from binascii import a2b_hex # If you need to 2 Base code save registration code and registration file can be used binascii conversion 
class register:
def __init__(self):
self.Des_Key = "BHC#@*UM" # Key
self.Des_IV = "\x22\x33\x35\x81\xBC\x38\x5A\xE7" #  Since the set IV vector 
# To obtain C Coil serial number 
# use C The advantages of the volume serial number is short length, easy to operate, such as 1513085707 , but for C Disk formatting or reloading computer operations will be affected C Coil serial number. 
#win32api.GetVolumeInformation(Volume Name, Volume Serial Number, Maximum Component Length of a file name, Sys Flags, File System Name)
#return('', 1513085707, 255, 65470719, 'NTFS'),volume serial number is 1513085707.
def getCVolumeSerialNumber(self):
#print chardet.detect(str(CVolumeSerialNumber))
#print CVolumeSerialNumber
if CVolumeSerialNumber:
return str(CVolumeSerialNumber) #number is long type . has to be changed to str for comparing to content after.
return 0
# use DES add base64 Form encryption 
# Have considered using M2Crypto and rsa But it's all because of being here windows Bad installation configuration process in the environment 
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) # turn base64 Code returned 
#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 DecryptStr
return DecryptStr
# Get the registration code and generate the registration file after successful verification 
def regist(self):
key = raw_input('please input your register code: ')
# Because the input is like" 12 "That doesn't fit base64 Strings of rules cause exceptions, so you need to add input judgment 
#while key
if 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 str
#print chardet.detect(key_decrypted)
#print key_decrypted
#type(key_decrypted) is str
if content!=0 and key_decrypted!=0:
if content != key_decrypted:
print "wrong register code, please check and input your register code again:"
elif content==key_decrypted:
print "register succeed."
# Read and write files to add judgment 
with open('./register','w') as f:
return True
return False
return False
return False
def checkAuthored(self):
checkAuthoredResult = 0
# Read and write files to add judgment 
if f:
if key:
if key_decrypted:
if key_decrypted == content:
checkAuthoredResult = 1
checkAuthoredResult = -1
checkAuthoredResult = -2
checkAuthoredResult = -3
except IOError:
print IOError
print checkAuthoredResult
return checkAuthoredResult
if __name__ == '__main__':

3. Note

1. The reason for using C volume serial number instead of hard disk number is that it is short and easy to operate.

But it is safer to use the hard disk number, which will not be changed by reinstalling the system, formatting the C disk, or changing the C disk number.

#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 hard disk number). The process of obtaining the complete hard disk number is as follows:

# Although using wmi You can get the disk serial number, but the disk serial number is 3253533258**************3237204620202020 , the encryption is too long, inconvenient to operate, so discard 
import wmi
def getHardDiskNumber(self):
c = wmi.WMI()
for physical_disk in c.Win32_DiskDrive():
return physical_disk.SerialNumber

3. chardet can be used to verify the encoding type of a string. It can be used to detect the equality of a string


4. There are still some logical loopholes, such as the judgment of whether the file exists when reading and writing, and the choice of reading method

5. for main functions or other functions that need to obtain the authentication status.

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

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

6. The administrator should also have an for encrypting the C volume serial number sent by the user using the des+base64 algorithm. After encrypting, a string or a registration file will be generated and returned to the user.

Related articles: