菜单

Aleer
Aleer
发布于 2026-11-24 / 0 阅读
0
0

CRYPTO - 三趟

考点:Shamir 三趟协议、交换加密、字典式已知明文攻击、哈希到群、AES-256-GCM

一、题目与附件

题面讲的是一个「互不想让对方看见歌单」的场景:用两把锁来回寄三次(我加锁 → 你加锁 → 我解锁 → 你解锁),最后双方得到共同喜欢的歌。中间人看不到内容,但知道了两把锁的齿数,而且齿数互素。附件里留下了她记录的三趟数据、两把锁的齿数、候选歌名、我的歌单、加密的 flag,以及她写的小脚本。

文件

内容

params.json

1024 bit 模数 N,两个指数 a、b(a 为 64 bit,b 为 59 bit,gcd(a,b)=1)

transcript.txt

2000 行,每行 3 个十六进制大整数(各 256 字节)

candidates.txt

3000 个候选歌名

my_playlist.txt

我的 300 首歌单

flag.bin

150 字节加密数据

her_codec.py

歌名→群元素、交集→AES 密钥的小脚本

her_codec.py 给出两个关键定义:

def to_group(title, N):          # 歌名 -> Z_N^* 中的元素
    for salt in range(1000):
        h = hashlib.sha256((title if salt == 0 else f"{title}#{salt}").encode()).hexdigest()
        m = int(h, 16) % N
        if m > 1 and gcd(m, N) == 1:
            return m
​
def key_of(titles):              # 交集 -> flag.bin 的 AES-256-GCM 密钥
    return hashlib.sha256("|".join(sorted(titles)).encode()).digest()

二、协议还原

Shamir 三趟协议利用模幂的交换性:

(m^a)^b = (m^b)^a = m^(ab)  (mod N)

标准流程是:Alice 发 m^a;Bob 发 m^(ab);Alice 去掉自己的锁发 m^b;Bob 用自己的锁解开得 m。

中间人能看到三次传递的密文,但通常无法反推 m(离散对数难)。本题的突破口是:中间人拿到了 a、b,以及一份候选歌名表。 明文空间可枚举 + 加密指数公开 ⇒ 可以对每个候选歌名做正向计算并比对密文,也就是一次字典式已知明文攻击,完全不需要分解 N,也不需要求 a⁻¹、b⁻¹。

三、确定三趟记录

先对 transcript.txt 第一行做关系测试(设三列为 c0,c1,c2):

pow(c0, b, N) == c2   # True
pow(c1, a, N) == c2   # True

其余任意两列之间的 ^a、^b、^(ab) 组合都不成立。因此:

  • c0 = m^a:她用自己锁加密后的单锁密文;

  • c1 = m^b:解掉 her 的锁之后、只剩他锁的单锁密文;

  • c2 = m^(ab):两把锁同时加上的双锁密文。

(三列不是按传递时间顺序排列,而是按「单锁 a / 单锁 b / 双锁」排列。)

于是判定某个候选歌名 t 是否在她歌单里,只需:

m = to_group(t, N)
pow(m, a, N) in {row[0] for row in transcript}   # 且
pow(m, b, N) in {row[1] for row in transcript}

两个条件同时满足才收,用来互相校验、避免误判。

四、恢复她的歌单与交集

枚举 3000 个候选歌名,共匹配出 120 首她的歌。再与我的 300 首歌单取交集,得到 12 首共同喜欢的歌:

三月汽水·二    下课窗·十三    光窗·十二      凌晨便利店·六
台风汽笛·十    圣诞信·四      圣诞汽笛·二    圣诞阁楼·十一
旧来信·十一    海汽水·五      秋天耳机·五    第七沙漏·三

按 key_of 的规则(Unicode 排序后以 | 连接,再 SHA-256)得到 AES-256 密钥:

b0494713e0ec80256d6b00eda145f1dc6d422ac1f4c5c7adeb317b0374c0fe37

五、解密 flag.bin

flag.bin 共 150 字节,布局为 12 字节 nonce + 16 字节 tag + 122 字节密文。用上面的密钥做 AES-256-GCM 解密并校验 tag,成功得到:

我们共同喜欢的歌,一共 12 首。第一首是《三月汽水·二》。PCTF{two_locks_with_coprime_teeth_cancel}

六、Flag

PCTF{two_locks_with_coprime_teeth_cancel}

七、完整解题脚本

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""三趟 (Shamir three-pass) 解题脚本。"""
import hashlib
import json
from math import gcd

from Crypto.Cipher import AES

BASE = "E:/WorkBuddy_Home/三趟_work/attachments/"

params = json.load(open(BASE + "params.json"))
N = int(params["N"], 16)
a = int(params["a"], 16)
b = int(params["b"], 16)

rows = [tuple(int(x, 16) for x in line.split())
        for line in open(BASE + "transcript.txt", encoding="utf-8").read().splitlines()]
# 每行三列分别是 m^a、m^b、m^(ab):col0^b == col2,col1^a == col2
locked_a = {row[0] for row in rows}
locked_b = {row[1] for row in rows}


def to_group(title):
    for salt in range(1000):
        h = hashlib.sha256(
            (title if salt == 0 else f"{title}#{salt}").encode()
        ).hexdigest()
        m = int(h, 16) % N
        if m > 1 and gcd(m, N) == 1:
            return m
    raise RuntimeError(title)


candidates = open(BASE + "candidates.txt", encoding="utf-8").read().splitlines()
hers = []
for title in candidates:
    m = to_group(title)
    if pow(m, a, N) in locked_a and pow(m, b, N) in locked_b:
        hers.append(title)

mine = set(open(BASE + "my_playlist.txt", encoding="utf-8").read().splitlines())
intersection = sorted(set(hers) & mine)
print("共同喜欢的歌:", intersection)

key = hashlib.sha256("|".join(intersection).encode()).digest()
blob = open(BASE + "flag.bin", "rb").read()
nonce, tag, ct = blob[:12], blob[12:28], blob[28:]
plaintext = AES.new(key, AES.MODE_GCM, nonce=nonce).decrypt_and_verify(ct, tag)
print(plaintext.decode("utf-8"))


评论