Python HTML实体加密问题
下面的代码对在代码底部输入的文件进行加密。注释应解释代码的作用。每次运行代码时,文件都会被值Python HTML实体加密问题,python,html-entities,encryption-symmetric,Python,Html Entities,Encryption Symmetric,下面的代码对在代码底部输入的文件进行加密。注释应解释代码的作用。每次运行代码时,文件都会被值shift覆盖。有人知道我如何达到评论中描述的预期结果吗 import random import html #Defines text map for characters to traverse. text_map = [] for i in range(9000): text_map.append(html.unescape("&#" + str(i) + &q
shift
覆盖。有人知道我如何达到评论中描述的预期结果吗
import random
import html
#Defines text map for characters to traverse.
text_map = []
for i in range(9000):
text_map.append(html.unescape("&#" + str(i) + ";"))
#Defines shift amount for each individual character in file.
shift = random.randint(2, len(text_map))
#Encription code:
if input("Do you want to encrypt a file? [y/n] ") == "y":
#Clears final encrypted file.
encrypted_file = []
#Encrypt function.
def encrypt(file_name):
#Read all lines of file.
file_lines = open(file_name, "r").readlines()
#For each line in the file...
for line in file_lines:
#For each character of each line in the file...
for character in line:
#Defines current position of character being encrypted.
curr_char = character
#Shifts every individual character in file by a certain amount.
for _ in range(shift):
#Handles list traversal overlap. (Character at end of list? => Bring to beginning of list.)
if curr_char == text_map[-1]:
curr_char = text_map[0]
else:
curr_char = text_map[text_map.index(curr_char) + 1]
#Appends each shifted character to a new file.
encrypted_file.append(curr_char)
#Cleans new encrypted file and overwrites the original file.
open(file_name, "w").write("".join(encrypted_file))
open(file_name, "a").write(str(shift))
print("File encrypted.")
#Calls encrypt function based upon user input.
encrypt(input("Enter file name to encrypt: "))