# 101 DES 250 points

## 题意

All the plain are encrypted 101 times with DES-ECB using the same key. So as the flag. The last line of the attachment is the flag cipher.

Attachment: ciphertexts.zip

## 解题步骤

读入 ciphertexts.txt，前面是若干的 (原文，密文) 组合，最后是 `flag` 的密文。编写程序发现，有这样的情况：

- M(A) = B
- M(B) = A

其中 M 表示 DES 加密。

这样的数据为：

```
26298ac60cf8e92c  ->  a0925806a2416035
a0925806a2416035  ->  26298ac60cf8e92c
```

会出现这种情况的只有 DES 的 Week Keys。进行枚举，即可得到 `Flag: THUCTF{The_k3y_1s_6ad..}` （[cipher.py](<https://jia.je/ctf-writeups/2018-09-28-thuctf2018/crypto/cipher.py>)）：

```
#!/usr/bin/env python3
# https://github.com/VoidHack/write-ups/tree/master/SharifCTF%208/crypto/DES
from Crypto.Cipher import DES
import sys
cipher = "8fb92ee53079e973327c73c3e779a010257806e32ef35f38"
key = ["0101010101010101", "FEFEFEFEFEFEFEFE", "E0E0E0E0F1F1F1F1", "1F1F1F1F0E0E0E0E"]
mapping = dict()
with open('ciphertexts.txt', 'r') as fd:
    lines = fd.readlines()
    for line in lines:
        plain = line[:16]
        c = line[17:-1]
        mapping[plain] = c
weak = None
for i in mapping.keys():
    c = mapping[i]
    if c in mapping:
        print i, ' -> ', c 
        print c, ' -> ', mapping[c]
        weak = c
# weak keys!
for i in key:
    des = DES.new(i.decode('hex'), DES.MODE_ECB)
    cur = weak.decode('hex')
    try:
        for j in range(0, 101):
            cur = des.decrypt(cur)
    except:
        print sys.exc_info()
    if cur == mapping[weak].decode('hex'):
        for k in range(0, 3):
            cur = cipher[k*16:(k+1)*16].decode('hex')
            try:
                for j in range(0, 101):
                    cur = des.decrypt(cur)
            except:
                print sys.exc_info()
            print cur
```
