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

Source: Internet
Author: User
Tags base64 decrypt string back

Http://www.cnblogs.com/cquptzzq/p/5940583.html

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.ImportBase64ImportWin32APIFrom PydesImport *#From binascii import A2b_hex #如果需要用二进制编码保存注册码和注册文件可以使用binascii转换ClassRegisterDef__init__(self): self. Des_key ="Bhc#@*um"#Key self. 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 CvolumeserialnumberIfCvolumeserialnumber:Return str (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 environmentsDefDesencrypt (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 encoding back#Des decodingDefDesdecrypt (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 generatedDefRegist (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 keyIfKey: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 str key_decrypted=STR (self. Desdecrypt (Base64.b64decode (key)))#Print Chardet.detect (key_decrypted)#Print key_decrypted#Type (key_decrypted) is strIf content!=0and 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 and write files to be judged with 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 judgedTry: F=open (‘./register‘,‘R‘)Iff:key=F.read ()IfKey:key_decrypted=Self. Desdecrypt (Base64.b64decode (key))IfKey_decrypted:if key_decrypted = =Content:checkauthoredresult = 1Else: Checkauthoredresult = 1 Else: Checkauthoredresult = 2 else: Checkauthoredresult = 3 E LSE: Self.regist () except IOError: print IOError print checkauthoredresult return checkauthoredresultif __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 WMIdef getharddisknumber (self):    c = WMI. WMI () in    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.