Python implementation of the software registration function (machine code + registration code mechanism)

Source: Internet
Author: User
Tags base64 decrypt string back

Objective: To complete the registration function function of Python image processing tool: After the user runs the program, it automatically detects the status of the authentication, if it is not certified, it needs to register. The registration process is the machine code that the user displays after the program is run (the volume sequence number of the C-drive) is sent back to the administrator, and the administrator generates an encrypted file or string returned to the user after encrypting the machine code. Each startup program, in the case of registered files, the program will be decoded through Des and Base64, and the right to obtain the C-Volume serial number comparison, if consistent run the main program. If the registration file is decoded and inconsistent with the volume serial number, it is necessary to remind the user to enter the registration code, if the new input after decoding and re-acquired machine code consistent, then through authentication, generate a new registration file after entering the main program. Libraries and Components: 1, Pydes used for encryption and decryption 2, base64, for Pydes encryption decryption two times encryption decryption 3, WIN32API, for the C volume serial number 4, Pyinstaller, packaging Reference: 1, pydes Library Implement Python's des encryption http://www.mamicode.com/info-detail-508384.htmlhttp://twhiteman.netfirms.com/des.html2, WIN32API. GETVOLUMEINFORMATIONHTTP://TIMGOLDEN.ME.UK/PYWIN32-DOCS/WIN32API__GETVOLUMEINFORMATION_METH.HTML3, Pyinstaller Packaging File Description Http://pythonhosted.org/PyInstaller/spec-files.html#using-spec-files second, realize
#Coding:utf8#register.py#function Description: After the user runs the program, automatically detects the status of authentication, if not certified, you need to register. The registration process is the machine code (the volume sequence number) that the user displays after the program is run, sent back to the administrator, who generates an encrypted file or string back to the user after encryption. #each login, in the case of registration files or registration code, the software will be decoded through Des and Base64, if the decoding and re-acquired machine code consistent, then through the authentication, into the main program. ImportBase64ImportWin32API fromPydesImport*#From binascii import A2b_hex #如果需要用二进制编码保存注册码和注册文件可以使用binascii转换classRegister:def __init__(self): self. Des_key="Bhc#@*um" #KeySelf. Des_iv ="\x22\x33\x35\x81\xbc\x38\x5a\xe7" #self-setting IV vector        #get the C-Reel serial number    #the advantage of using the C-Reel serial number is the short length, convenient operation, such as 1513085707, but the C-drive format or the reloading of the computer and other operations will affect the C-reel serial number.     #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.    defGetcvolumeserialnumber (self): Cvolumeserialnumber=win32api. GetVolumeInformation ("c:\\") [1]        #Print Chardet.detect (str (cvolumeserialnumber))        #Print Cvolumeserialnumber        ifCvolumeserialnumber:returnSTR (cvolumeserialnumber)#Number is a long Type,has to being changed to STR for comparing to content after.        Else:            return0#use des plus base64 as a form of encryption    #considered using M2crypto and RSA, but were discarded because of the poor installation configuration process in Windows environments    defdesencrypt (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))        returnBase64.b64encode (ENCRYPTSTR)#turn base64 encoding back        #des decoding    defDesdecrypt (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))        PrintDecryptstrreturnDecryptstr#Obtain the registration code, verify the success of the registration file generated    defregist (self): Key= Raw_input ('Please input your register code:')        #because entering a string like "12" that does not conform to the Base64 rule causes an exception, it is necessary to increase the input judgment        #While key        ifkey:content= Self.getcvolumeserialnumber ()//Number have 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 str            ifContent!=0 andkey_decrypted!=0:ifContent! =key_decrypted:Print "Wrong register code, please check and input your register code again:"self.regist ()elifcontent==key_decrypted:Print "register succeed."                    #read and write files to be judgedWith open ('./register','W') as F:f.write (key) F.close ()returnTrueElse:                    returnFalseElse:                returnFalseElse: Self.regist ()returnFalsedefcheckauthored (self): content=self.getcvolumeserialnumber () Checkauthoredresult=0#read and write files to be judged        Try: F=open ('./register','R')            ifF:key=F.read ()ifkey:key_decrypted=Self . Desdecrypt (Base64.b64decode (key))ifkey_decrypted:ifkey_decrypted = =Content:checkauthoredresult= 1Else: Checkauthoredresult=-1Else: Checkauthoredresult=-2Else: Checkauthoredresult=-3Else: Self.regist ()exceptIOError:PrintIOErrorPrintCheckauthoredresultreturnCheckauthoredresultif __name__=='__main__': Reg=Register () reg.regist ()

Third, note 1, the use of C-reel serial number instead of the hard drive number is the reason: the number of bits is short, convenient operation. However, it is safer to use a hard drive number because the hard drive number is not changed by reloading the system, formatting the C drive, or modifying the C-drive sequence 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 a full hard drive number is as follows:#虽然使用wmi可以获取磁盘序列号, but the disk serial number is 3253533258**************3237204620202020, after encryption is too long, inconvenient operation, so discard
Import WMI def Getharddisknumber (self):     = WMI. WMI ()    for in  c.win32_diskdrive ():    return physical_disk. SerialNumber

HTTPS://PYPI.PYTHON.ORG/PYPI/WMI/3, Chardet can be used to verify the encoding type of the string, can be used to detect string equality on Chardet.detect (str) 4, there are some logical vulnerabilities, For example, read and write files on the existence of the judgment, the choice of reading methods, such as 5, register.py, for the main function or other need to obtain the authentication status of the function call. The process of using the Register class in the main function is to create a login function to obtain the authentication result--"If the authentication result is false, re-call the Register class Regist function, 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, 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 encryption, encryption after the generation of strings or registration files, and then returned to the user, no longer repeat.

Reprint please specify the source:

http://www.cnblogs.com/cquptzzq/

Python implementation of the software registration function (machine code + registration code mechanism)

Related Article

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.