python(5).zip
立即下载
资源介绍:
python(5).zip
def build_cipher_map(key):
"""构建加密字母映射表"""
alphabet = 'abcdefghijklmnopqrstuvwxyz'
unique_key = ""
for char in key:
if char not in unique_key:
unique_key += char
reverse_alphabet = ''.join([char for char in alphabet[::-1] if char not in unique_key])
cipher = unique_key + reverse_alphabet
cipher_map = {}
for i in range(len(alphabet)):
cipher_map[alphabet[i]] = cipher[i]
return cipher_map
def encrypt_file(input_filename, output_filename, cipher_map):
"""加密文件内容并写入新文件"""
with open(input_filename, 'r', encoding='utf-8') as infile:
content = infile.read()
encrypted_content = ""
for char in content:
if char in cipher_map:
encrypted_content += cipher_map[char]
else:
encrypted_content += char
with open(output_filename, 'w', encoding='utf-8') as outfile:
outfile.write(encrypted_content)
def main():
key = input(" ")
cipher_map = build_cipher_map(key)
encrypt_file('encrypt.txt', 'output.txt', cipher_map)
print("")
if __name__ == "__main__":
main()