Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use python to realize super encryption software

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use python to achieve super encryption software. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Steps

Import the library first

Import osimport hashlib

Reading and writing to a file

A = open (name_1, "rb") # read file b = open (data, "wb") # write file a.close () # Save exit b.close () # Save exit

Use ord for encoding, bytes for decoding, built-in index for cyclic reading of passwords, and one by one for encoding and writing

# encryption and decryption def Encryption_and_decryption (): count = 0 # index for now in a: for nowByte in now: newByte = nowByte ^ ord (password_ data [count% len (password_data)]) # loop traverses the order value of the password, and a single loop count + = 1 b.write (bytes ([newByte])) # converts Encryption_and_decryption ()

Using ord to encode, we can see that there is a problem with encrypted files, such as text files. If the encrypted password is' qwer', encoding, the qwer will be encoded as 113119101 114 with bytes respectively, and then the encoded numbers will be inserted into the text one by one in the loop instead. Decryption is the same principle, but there is a fatal flaw, that is, if your password is "qwer". When decoding, enter a password Q to decode all of them successfully, which is a defect.

So I use MD5 to encrypt, and then encode. MD5, as we all know, different MD5 values are converted by different combinations of letters, numbers and strings, and MD5 is also case-sensitive, which makes our encryption software more secure.

Hl = hashlib.md5 () hl.update (password.encode (encoding='utf-8')) password_list = hl.hexdigest ()

For our data, I encrypt MD5 twice, encrypt it again with the encrypted MD5 value, and then combine the first MD5 value with the second MD5 value (non-additive) to form a high-intensity encryption.

# encryption using MD5 (double-layer encryption) hl = hashlib.md5 () hl.update (password.encode (encoding='utf-8')) password_list = hl.hexdigest () hl.update (password_list.encode (encoding='utf-8')) password_list2 = hl.hexdigest () password_data = password_list+password_list2

Decoding is the same principle, the password will be encrypted and then encrypted, and then enter the file for encoding conversion, if the password is correct, the file will not have garbled, if the password is wrong, the file is garbled, this program is no hint of successful decoding and encryption.

Because of the variety of paths, the program will run errors, so I use replace to convert the path'\ / 'and convert all'\ 'and'\'to'/ 'for easy reading of the program.

Name_1 = name_1.replace ("\\", "/") # replace data = data.replace ("\\", "/") # replace

In order to facilitate everyone to use, extract the program to detect whether there is an error in the file or path.

If os.path.exists (name_1) = = True: passelse: print ('Please check if the path is incorrect or the file does not exist!!') Os.system ('pause') exit ()

The saved path is indispensable. If the saved path is skipped directly without input, it will default to the location of the reading program. If the path of the reading program is not written, it will be saved in the root directory of the reading program.

If name_1.split (".") [1] [- 4:] = 'DATA': F = name_1.split (".") [1] .replace ("DATA") "") if os.path.split (data) [0] ='': if os.path.split (name_1) [0] ='': data = os.path.split (name_1) [- 1] .split (".") [0] +'. + F else: data = os.path.split (name_1) [0] +'/'+ os.path.split (name) _ 1) [- 1] .split (".") [0] +'.'+ Felse: data = data +'/'+ os.path.split (name_1) [- 1] .split (".") [0] +'. Felse: # save path if os.path.split (data) [0] = =': if os.path.split (name_1) [0] ='': Data = name_1.split (".") [1] # suffix data = os.path.split (name_1) [- 1] .split (".") [0] +'. + data + 'DATA' else: data = name_1.split (".") [1] # suffix data = os.path.split (name_1) [0] +' /'+ os.path.split (name_1) [- 1] .split (".") [0] +'.'+ data + 'DATA' else: name_3 = name_1.split (".") [1] # suffix data = data +' /'+ os.path.split (name_1) [- 1] .split (".") [0] +'.'+ name_3 + 'DATA'

After the program is encrypted, it will appear DATA in the suffix of the program. This is to make it easy to know what the encrypted program is, and it also allows the software program to detect whether it is an encrypted program and facilitate decryption.

Complete code: import osimport hashlib print ("--software encryption tool--") print ("pay attention to bloggers not to get lost! \ nUse https://jiangongfang.blog.csdn.net/\nhttps://blog.51cto.com/u_15449377")print(" to inform:\ n [the encrypted file suffix will have more DATA for software detection. Please do not change the encrypted suffix name]) print ("[the default path to save the file is the current directory of the encrypted file or decrypted file). Is not the current directory of the software]) print ("[the name of the file to be encrypted or decrypted cannot have the character". /\ " Otherwise it will make an error] ") print ("-- Software encryption tool--\ n ") name_1 = input ('enter the file name to be encrypted or decrypted with the suffix:') # determine whether the file if os.path.exists (name_1) = = True: passelse: print ('Please check whether the path is incorrect or the file does not exist!!') Os.system ('pause') exit () password = input (' Please enter the password to encrypt or decrypt:') data = input ('enter the path location of the file to be saved (optional):') name_1 = name_1.replace ("\\", "/") # replace data = data.replace ("\\") "/") # replace if name_1.split (".") [1] [- 4:] = 'DATA': F = name_1.split (".") [1] .replace ("DATA" "") if os.path.split (data) [0] ='': if os.path.split (name_1) [0] ='': data = os.path.split (name_1) [- 1] .split (".") [0] +'. + F else: data = os.path.split (name_1) [0] +'/'+ os.path.split (name) _ 1) [- 1] .split (".") [0] +'.'+ Felse: data = data +'/'+ os.path.split (name_1) [- 1] .split (".") [0] +'. Felse: # save path if os.path.split (data) [0] = =': if os.path.split (name_1) [0] ='': Data = name_1.split (".") [1] # suffix data = os.path.split (name_1) [- 1] .split (".") [0] +'. + data + 'DATA' else: data = name_1.split (".") [1] # suffix data = os.path.split (name_1) [0] +' /'+ os.path.split (name_1) [- 1] .split (".") [0] +'.'+ data + 'DATA' else: name_3 = name_1.split (".") [1] # suffix data = data +' /'+ os.path.split (name_1) [- 1] .split (".") [0] +'.'+ name_3 + 'DATA' a = open (name_1 "rb") # read file b = open (data "wb") # write file # encrypt using MD5 (double layer encryption) hl = hashlib.md5 () hl.update (password.encode (encoding='utf-8')) password_list = hl.hexdigest () hl.update (password_list.encode (encoding='utf-8')) password_list2 = hl.hexdigest () password_data = password_list+password_list2 # encryption and decryption def Encryption_and_decryption (): count = 0 # index For now in a: for nowByte in now: newByte = nowByte ^ ord (password_ data [count% len (password_data)]) # Loop through the order value of the password Single loop count + = 1 b.write (bytes ([newByte])) # convert Encryption_and_decryption () a.close () b.close () os.system ('pause')

You can see that the encrypted program has the suffix DATA, and the original suffix name will be restored after decryption.

This is the end of this article on "how to use python to achieve super encryption software". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 259

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report