程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python makes a super strong encryption software

編輯:Python

Use python Make a software for encrypting data , Encrypted applications 、 file 、 Compressed package and other file formats , Do not encrypt folders directly , You can use the compressed package to package in the encrypted file first . The suffix of the encrypted file cannot be changed randomly , Otherwise, decryption will fail . There is no prompt message of successful encryption and decryption in the program .

Encryption uses two layers MD5 encryption

Import library first author :

import os
import hashlib

Read files and write files

a = open(name_1, "rb") # Read the file
b = open(data, "wb") # write file
a.close() # Save and exit
b.close() # Save and exit 

Use ord Encoding , use bytes decode , The built-in index is used to read the password circularly , Read and write one by one

# Encryption and decryption
def Encryption_and_decryption():
count = 0 # Indexes
for now in a:
for nowByte in now:
newByte = nowByte ^ ord(password_data[count % len(password_data)]) # Loop through the password ord value , Single cycle
count += 1
b.write(bytes([newByte])) # transformation
Encryption_and_decryption()

use ord Coding can see that there will be a little problem , Is the encrypted file , As a text file , If the encrypted password is ‘qwer’, Coding will q w e r Use them separately bytes Encoded as 113 119 101 114, Then insert the encoded numbers into the text one by one in the loop , To replace , Decryption works the same way , But there is a fatal flaw , If your password is ”qwer“, Enter a password when decoding q You can decode them all successfully , This is a defect .

So I use MD5 To encrypt , And then code it ,MD5 Everybody knows , Different MD5 Values are composed of different letters 、 Numbers 、 String combination is converted into ,MD5 Also case sensitive , This also makes our encryption software more secure

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

For our information , I will MD5 Secondary encryption , Use the encrypted MD5 Value is encrypted again , And then the first one MD5 Value and the second MD5 Values to combine ( Non additive ), Form a high-strength encryption

# Use MD5 To encrypt ( 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 works the same way , Change the password MD5 Encrypt and then encrypt , Then enter the file for encoding conversion , If the password is correct, the file will not be garbled , If the password is wrong, the file is full of garbled code , This program does not prompt the success of decoding and encryption .

Due to the variety of paths , The program will run incorrectly , So I use it replace The path ’\ /‘ transformation , Put all the ’\\‘ and ’\‘ Convert to ’/‘, For easy program reading

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

In order to facilitate everyone's use , Extract for program detection , Check whether the file exists , Or path error

if os.path.exists(name_1) == True:
pass
else:
print(' Please check whether the path is wrong 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 reader , If the path of the reader is not written , It will be saved in the root directory of the reader

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] + '.' + F
else:
data = data + '/' + os.path.split(name_1)[-1].split(".")[0] + '.' + F
else:
# Save the 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 this program is encrypted, it will appear in the program suffix DATA, This is to facilitate a clear understanding of what is an encrypted program , It also allows the software program to detect whether it is an encrypted program , Easy to decrypt .

Complete code :

import os
import hashlib
print("------------------------------------- Software encryption tools -------------------------------------")
print(" Use to inform :\n【 Encrypted files will have more suffixes DATA, To facilitate software detection , Do not change the encrypted suffix 】")
print("【 The default path to save the file The current directory of encrypted or decrypted files , Not the current directory of the software 】")
print("【 The name of the file to be encrypted or decrypted cannot have “./\” character , Otherwise it will go wrong 】")
print("------------------------------------- Software encryption tools -------------------------------------\n")
name_1 = input(' Enter the file name to be encrypted or decrypted, including the suffix :')
# Determine if the file exists
if os.path.exists(name_1) == True:
pass
else:
print(' Please check whether the path is wrong 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 and location where you want to save the file ( Not required ):')
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] + '.' + F
else:
data = data + '/' + os.path.split(name_1)[-1].split(".")[0] + '.' + F
else:
# Save the 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 the file
b = open(data, "wb") # write file
# Use MD5 To encrypt ( 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 # Indexes
for now in a:
for nowByte in now:
newByte = nowByte ^ ord(password_data[count % len(password_data)]) # Loop through the password ord value , Single cycle
count += 1
b.write(bytes([newByte])) # transformation
Encryption_and_decryption()
a.close()
b.close()
os.system('pause') 

You can see that the suffix of the encrypted program has DATA, After decryption, the original suffix will be restored

Finally, I wish you progress every day !! Study Python The most important thing is mentality . We are bound to encounter many problems in the process of learning , Maybe you can't solve it if you want to break your head . It's all normal , Don't rush to deny yourself , Doubt yourself . If you have difficulties in learning at the beginning , Looking for one python Learning communication environment , You can join us , Get the learning materials , Discuss together

 

 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved