首页 景点排名文章正文

用Python代码实现情报传递:致敬《沉默的荣耀》

景点排名 2025年10月20日 17:23 0 admin

手指敲下最后一行代码时,突然想起那些用体温保护真空管的地下工作者。

今天我们用电风扇给电脑降温,他们用生命给电台保温。

用Python复现情报加密过程,像是完成一场跨越时空的致敬。

第一步:打造数字密码本

真正的书本密码需要实体书,我们用字典代替。原理很简单:每个字对应一组数字坐标。假设用《红楼梦》当密码本,"宝玉"在第36页第3行第5字,就记作36-3-5。

cipher_book = {    "转移": "36-3-5",  # 实际应用中应该全书映射    "军火": "58-12-8",    "暴露": "121-7-2"}

但这样太容易被破解。地下工作者的智慧在于动态密码——每天更换偏移量。实现起来很简单:

import datetime# 获取当日日期作为密钥today = datetime.date.today()offset = today.day  # 用日期当偏移量def dynamic_cipher(text):    if text not in cipher_book:        return None    page, line, char = cipher_book[text].split('-')    # 每页加偏移量,行和字取模防止溢出    new_page = int(page) + offset    new_line = (int(line) + offset) % 20  # 假设每页20行    new_char = (int(char) + offset) % 50  # 假设每行50字    return f"{new_page}-{new_line}-{new_char}"

这样今天"转移"可能是"39-6-8",明天变成"40-7-9"。就算敌人拿到密码本,不知道当天日期也白搭。

第二步:莫尔斯电码转换

滴滴答答的电波声,其实是情报的第二次加密。Python实现莫尔斯编码器只要几行:

morse_code = {    'A': '.-', 'B': '-...', '0': '-----',    '1': '.----', ' ': '/'}  # 完整版需补全字母数字def text_to_morse(message):    return ' '.join(morse_code.get(char.upper(), '') for char in message)# 示例:发送SOSprint(text_to_morse("SOS"))  # 输出: ... --- ...

但地下工作者从不会直接发明文。他们会把动态密码本生成的数字串,比如"36-3-5",转换成莫尔斯码再发送。双重加密,双重保险。

第三步:伪装成商业电报

这是最精妙的部分。发送"棉纱300匹"比发一串数字安全得多。我们模拟这个伪装系统:

commercial_dict = {    "36-3-5": "棉纱到货",    "58-12-8": "瓷器破损",    "121-7-2": "豆油缺货"}def commercial_mask(code):    return commercial_dict.get(code, "市场正常")# 实战示例secret_code = dynamic_cipher("转移")  # 假设当天输出"39-6-8"masked_message = commercial_mask("39-6-8")  # 映射为"棉纱到货"morse_signal = text_to_morse(masked_message)  # 最终发送的莫尔斯码

收到情报的人按反向流程操作:先收莫尔斯电码转文字,再查商业电报对照表获取密码,最后用当天偏移量反向解密密码本坐标。

完整流程测试

# 发送端def send_intelligence(action):    code = dynamic_cipher(action)    if not code:        print("情报错误")        return    commercial = commercial_mask(code)    morse = text_to_morse(commercial)    print(f"发送伪装情报: {commercial}")    print(f"莫尔斯电码: {morse}")    return morse# 接收端def receive_intelligence(morse, offset):    # 莫尔斯转文字    morse_reversed = {v: k for k, v in morse_code.items()}    words = ''.join(morse_reversed.get(code, '') for code in morse.split())    print(f"接收商业报文: {words}")    # 商业报文转密码    commercial_reversed = {v: k for k, v in commercial_dict.items()}    code = commercial_reversed.get(words)    if not code:        print("无效报文")        return    # 去除偏移量    page, line, char = code.split('-')    raw_page = int(page) - offset    raw_line = (int(line) - offset) % 20    raw_char = (int(char) - offset) % 50    raw_code = f"{raw_page}-{raw_line}-{raw_char}"    # 密码本查真实含义    action = [k for k, v in cipher_book.items() if v == raw_code]    print(f"解密情报: {action[0] if action else '未知指令'}")# 模拟今日通讯today_offset = datetime.date.today().daymorse_signal = send_intelligence("转移")print("\n--- 电波穿越时空 ---\n")receive_intelligence(morse_signal, today_offset)

运行结果可能是:

发送伪装情报: 棉纱到货莫尔斯电码: -- .. ... -. / -.. .- --- .... ------接收商业报文: 棉纱到货解密情报: 转移

代码背后的温度

写完这段代码已是深夜。看着终端里闪烁的光标,突然想到当年那些收报员。他们要在敌军搜查时十分钟内拆解电台藏进地板,要在寒冬用冻僵的手记录电码。我们今天按个回车就能完成的加密流程,是他们拿命守护的战线。

技术会老去,精神永流传。下次你写Python时,不妨试试这段代码。当莫尔斯码在屏幕亮起,那是穿越八十年的电波,在对你说:信念永不消逝。

发表评论

长征号 Copyright © 2013-2024 长征号. All Rights Reserved.  sitemap