def caesar_encrypt(text, shift):
result = ""
for char in text:
if char.isalpha():
if char.isupper():
result += chr((ord(char) - 65 + shift) % 26 + 65)
else:
result += chr((ord(char) - 97 + shift) % 26 + 97)
else:
result += char
return result
def process_large_file(file_path, shift):
try:
with open(file_path, 'r', encoding='utf-8') as file:
for line in file:
encrypted_line = caesar_encrypt(line.strip(), shift)
print(encrypted_line)
except FileNotFoundError:
print(f"文件 '{file_path}' 未找到。")
except Exception as e:
print(f"发生错误: {e}")
if __name__ == "__main__":
file_path = input("请输入文件路径: ")
try:
shift = int(input("请输入凯撒密码偏移量: "))
process_large_file(file_path, shift)
except ValueError:
print("偏移量必须是一个整数。")