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

Source: Internet
Author: User
Tags base64 decrypt string back python script

First, the foreword:

Objective: To complete the registration function of an existing Python image processing tool

Function: After the user runs the program, the authentication status is automatically detected by the file, if it is not certified, it needs to be registered. The registration process is the user to the program after the operation of the display of machine code (c-Disk volume serial number) sent back to the administrator, the administrator to encrypt the machine code after the generation of encrypted files or string returned to the user. Each time you start the program, in the case of a registered file, the program will decode through Des and Base64, and compare it to the C-volume serial number obtained at the moment, and run the main program if consistent. If the registration file is decoded and the volume serial number is inconsistent, it is necessary to remind the user to enter the registration code, if the new input after the decoding and retrieve the machine code consistent, then through authentication, generate a new registration file into the main program.

Libraries and Components:

1, Pydes for encryption and decryption

2, base64, used for pydes encryption and decryption after the encryption and decryption two times

3, Win32API, for obtaining C volume serial number

4, Pyinstaller, packing

Reference:

1, Pydes Library Implementation of 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, Pyinstaller packing document description

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

Second, realize

#coding: UTF8 #register. py #功能说明: After the user runs the program, automatically detect the status of authentication, if not certified, you need to register.
The registration process is that the user sends back the machine code (volume serial number) that is displayed after the program is run, and the administrator generates the encrypted file or string back to the user by encrypting it.
#每次登录, in the case of a registered file or registration code, the software will be decoded through Des and Base64, if the decoder and retrieve the machine code consistent, then through authentication, into the main program. Import Base64 import Win32API from pydes import * #from binascii import A2b_hex #如果需要用二进制编码保存注册码和注册文件可以使用binascii转换 class Register:def __init__ (self): self. Des_key = "Bhc#@*um" # Key self. Des_iv = "\x22\x33\x35\x81\xbc\x38\x5a\xe7" # custom IV vector #获取C盘卷序列号 #使用C盘卷序列号的优点是长度短, easy to operate, such as 1513085707,
However, the C disk volume serial number will be affected by operations such as formatting or reloading the computer. #win32api. GetVolumeInformation (Volume name, Volume serial number, Maximum Component Length of a file Name, Sys Flags, file System Na
Me) #return (', 1513085707, 255, 65470719, ' NTFS '), Volume serial number is 1513085707. def getcvolumeserialnumber (self): Cvolumeserialnumber=win32api. GetVolumeInformation ("c:\\") [1] #print chardet.detect (str (cvolumeserialnumber)) #print Cvolumeserialnumber if Cvolumeserialnumber:return Str (cvolumeserialnumber) #number is long Type,has to being changed to STR forComparing to the content after. Else:return 0 #使用DES加base64的形式加密 #考虑过使用M2Crypto和rsa, but the Def desencrypt (SELF,STR) was discarded because of the bad installation configuration process in the Windows environment: 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) #转base64编码返回 #des解码 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 R Eturn decryptstr #获取注册码 to generate the registered file Def regist (self) After successful validation: key = raw_input (' Please input your Register code: ') #由于输入类似 "12" this discrepancy The string that base64 the rule will cause an exception, so you need to increase the 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 key_d Ecrypted=str (self. Desdecrypt (Base64.b64decode (key)) #print Chardet.detect (key_decrypted) #print key_decrypted #type (key_decrypted) is str. 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. "#读写文件要加判断 With open ('./register ', ' W ') as F:f.write (key) F.close () return True Else:return false Else:return false Else:self.regi St () return False def checkauthored (self): Content=self.getcvolumeserialnumber () checkauthoredresult = 0 #读写文件要加判断 try:f =open ('./register ', ' r ') if F:key=f.read () if key:key_decrypted=self. Desdecrypt (Base64.b64decode (key)) if key_decrypted:if key_decrypted = = Content:checkauthoredresult = 1 Else:checkautho Redresult =-1 Else:checkauthoredresult =-2 Else:checkauthoredresult = 3 else:self.regist () except Ioerror:print IOE Rror Print Checkauthoredresult return checkauthoredresult if __name__ = = ' __main__ ': Reg=register () reg.regist ()

Third, notes

1, the use of C-volume serial number instead of the hard drive number is: The number of short, convenient operation.

However, it is safer to use a hard disk number because the hard drive number is not changed by reloading the system, formatting C, or modifying the C-disk serial 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), to obtain the full hard drive number of the process is as follows:

#虽然使用wmi可以获取磁盘序列号, but the disk serial number is 3253533258**************3237204620202020, the encryption is too long, inconvenient operation, so discard
import WMI
def 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 verify the string encoding type, can be used in the detection of string equality

Chardet.detect (str)

4, there are some logical loopholes, such as reading and writing files on the existence of documents, the choice of reading methods, etc.

5, register.py, for the main function or other needs to obtain authentication status of the function call.

The procedure for using the Register class in the main function is:

Create the login function to obtain the authentication result-if the authentication result is false, the Regist function of the register class is called back, reminding the user to enter the registration code, and only if the registration code is successfully entered can the new registration file be created-"If the authentication result is true, start the main program directly."

6, the administrator should also have a encryption.py, used to use the DES+BASE64 algorithm to the user sent over the C-volume serial number for encryption, encrypted after the generation of strings or registration files, and then returned to the user, no longer repeat.

The above is a small set up to introduce the software based on Python script registration function (machine code + registration code mechanism), I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.