import string
import random

def generate_substitution_table():
    """生成一个随机的单表替换表"""
    chars = list(string.ascii_lowercase)  # 仅使用小写字母
    shuffled_chars = chars.copy()
    random.shuffle(shuffled_chars)
    return dict(zip(chars, shuffled_chars))

def substitute_with_table(s, table):
    """使用提供的替换表对字符串进行替换"""
    return ''.join(table[char] if char in table else char for char in s)

def process_file_for_substitution(input_file, output_file):
    with open(input_file, 'r', encoding='utf-8') as f:
        content = f.read()
        
    substitution_table = generate_substitution_table()
    substituted_content = substitute_with_table(content, substitution_table)
    
    with open(output_file, 'w', encoding='utf-8') as f:
        f.write(substituted_content)
    return substitution_table



# 使用函数
input_file = "plaintext.txt"
output_file = "cipher.txt"
process_file_for_substitution(input_file, output_file)
