题目

bash
from secret import *
ciphertext = []
for f in flag:
ciphertext.append(f ^ key)
print(bytes(ciphertext).hex())
# e9e3eee8f4f7bffdd0bebad0fcf6e2e2bcfbfdf6d0eee1ebd0eabbf5f6aeaeaeaeaeaef2
解题思路
题目分析
核心:每个 flag 的字节,和同一个固定 key(单字节!)异或 ,也就是:
c i = f i ⊕ k c_i = f_i \oplus k ci=fi⊕k
变形:
f i = c i ⊕ k f_i = c_i \oplus k fi=ci⊕k
k k k 是一个 0~255 的字节(0x00 ~ 0xff)。
这是【单字节全局异或】,属于非常经典的简单 Crypto。
解题思路
密文 hex:
e9e3eee8f4f7bffdd0bebad0fcf6e2e2bcfbfdf6d0eee1ebd0eabbf5f6aeaeaeaeaeaef2
异或性质:
- 如果
c = f ^ k,那么k = c ^ f - flag 一般有固定开头,CTF 里最常见:
flag{
我们拿密文前 5 个字节和"flag{"试算 key。
"flag{" 的 ASCII:
f→ 0x66l→ 0x6ca→ 0x61g→ 0x67{→ 0x7b
密文前 5 字节:
- 0xe9
- 0xe3
- 0xee
- 0xe8
- 0xf4
计算第一个候选 key: k = 0 x e 9 ⊕ 0 x 66 k = 0xe9 \oplus 0x66 k=0xe9⊕0x66
直接写 Python 一次性爆破,下面是完整解题脚本:
python
cipher_hex = "e9e3eee8f4f7bffdd0bebad0fcf6e2e2bcfbfdf6d0eee1ebd0eabbf5f6aeaeaeaeaeaef2"
cipher = bytes.fromhex(cipher_hex)
# key 是单字节:0 ~ 255
for k in range(256):
plain = bytes([b ^ k for b in cipher])
# 判断是否是可见字符串,包含 flag{
try:
s = plain.decode("ascii")
if "flag{" in s:
print(f"found key = {k:#04x} ({k})")
print("flag:", s)
except:
continue
运行结果:
found key = 0x8f (143)
flag: flag{x0r_15_symm3try_and_e4zy!!!!!!}
✅ Key:0x8f(十进制 143)
✅ Flag:flag{x0r_15_symm3try_and_e4zy!!!!!!}
CyberChef快速出答案

作业:GKCTF 2021XOR

python
from os import urandom
from secret import flag
key = urandom(1)
def xor(plaintext, key):
ret = []
for i in range(len(plaintext)):
ret.append(plaintext[i] ^ key[0])
return bytes(ret)
ciphertext = xor(flag, key)
print(ciphertext.hex())
output.txt
bash
9b919c9a8685cd8fa294c8a28c88cc89cea2ce9c878480
解题思路

答案:flag{x0r_i5_qu1t3_3azy}