import string
from docx import Document
from openpyxl import load_workbook

def encrypt_file(input_file_path, output_file_path, key):
    # 根据输入文件的后缀选择合适的加密函数
    if input_file_path.endswith('.docx'):
        # 如果是.docx文件,使用Python库docx对文本进行加密
        document = Document(input_file_path)
        for paragraph in document.paragraphs:
            text = paragraph.text
            encrypted_text = encrypt_text(text, key) #调用encrypt_text函数进行加密操作
            paragraph.text = encrypted_text
        document.save(output_file_path)#保存加密后的文件
    elif input_file_path.endswith('.xlsx'):
        #如果是.xlsx文件,使用Python库openpyxl对单元格数据进行加密
        workbook = load_workbook(input_file_path)
        for sheet in workbook.sheetnames:
            worksheet = workbook[sheet]
            for row in worksheet.iter_rows():
                for cell in row:
                    if cell.value is None:
                        continue
                    if isinstance(cell.value, str):
                        encrypted_value = encrypt_text(cell.value, key)#调用encrypt_text函数进行加密操作
                        cell.value = encrypted_value
        workbook.save(output_file_path)#保存加密后的文件
    else:
        #如果是其他类型的文件,则按照转子密码进行加密
        #定义三个转子,用于进行加密操作
        rotor1 = list(string.ascii_lowercase)
        rotor2 = list(string.ascii_lowercase)
        rotor3 = list(string.ascii_lowercase)
        #根据提供的密钥对转子进行初始化
        for i in range(key[0]):
            rotor1.append(rotor1.pop(0))
        for i in range(key[1]):
            rotor2.append(rotor2.pop(0))
        for i in range(key[2]):
            rotor3.append(rotor3.pop(0))
        #存储加密后的结果数据
        result_data = []
        #打开输入文件进行读取数据
        with open(input_file_path, 'r') as file:
            data = file.read()
            #遍历输入文件的每个字符
            for character in data:
                if character.isalpha():#如果字符是字母
                    index = rotor1.index(character.lower())#在第一个转子中查找字母在转子中的位置
                    index = rotor2[index]#将结果作为索引在第二个转子中查找
                    index = rotor3[index]#将结果作为索引在第三个转子中查找
                    result_data.append(rotor1[index])#将最终的结果字母添加到结果数据中
                else:#如果字符不是字母,则直接添加到结果数据中
                    result_data.append(character)
        #将加密后的结果数据写入输出文件
        with open(output_file_path, 'w') as file:
            file.write(''.join(result_data))
    return True

函数说明:

  • encrypt_file(input_file_path, output_file_path, key)
    • input_file_path: 输入文件的路径
    • output_file_path: 输出文件的路径
    • key: 加密密钥,是一个包含三个整数的列表,用于初始化三个转子

使用方法:

  1. 导入必要的库:import string, from docx import Document, from openpyxl import load_workbook
  2. 定义加密密钥:key = [1, 2, 3]
  3. 调用 encrypt_file() 函数,传入输入文件路径、输出文件路径和加密密钥:
    • encrypt_file('input.docx', 'output.docx', key)
    • encrypt_file('input.xlsx', 'output.xlsx', key)
    • encrypt_file('input.txt', 'output.txt', key)

注意:

  • 确保你已安装 docxopenpyxl 库。
  • encrypt_text() 函数未在代码中定义,你需要根据你的需求实现该函数。
  • 转子密码的加密效果有限,主要用于演示目的。
Python 文件加密:支持 docx、xlsx 和自定义转子密码加密

原文地址: https://www.cveoy.top/t/topic/oRSI 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录