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UjNkTFZXeG9VMVl4V25SbFNHUnNWbXh3TUZSV1ZtdGhSa2w0VW1wYVZs
WXphSG9LVm0weFIyTnNaSFJoUmxwTwpVbTVDYUZkc1dsWmxSbHBYVW01T1YySlhlRmhXTUZaTFUx
WmFkR05GWkZaa00wSlRWRlphYzA1V1ZuUk9WWFJvVmxSQ05WWlhlRzlYClozQlhUVEZLYndwV2JY
QkhaREZaZUZwSVNsQldNMEp3Vm14YWQxTldXbkZUV0docVRWWldOVlV5TlV0V1IwcElZVVZXV21F
eGNETlUKVlZweVpERmFWVlpzWkdGTk1FcFFWbGQwVjFOck1VZGFSbFpTQ21KVlduQlVWM1IzVTBa
VmVVNVdUbGRpVlhCSlEyMVdSMXBHY0ZkTgpNVXB2VVRJeFIxSXhXbGxhUm1ocFYwWktlRmRYZEd0
Vk1sWnpXa2hLWVZKNmJGaFVWM1JYVG14V1dFMVZaRmNLVFZad01GWkhjRk5XCmJVWnlWMjFHWVZa
c2NFeFdNV1JMVWpGa2MyRkdUazVXV0VKSVZtcEdZV0l5VVhoWFdHZExWa2QwYTFZeFpFaGxTRXBX
WVdzMVZGbHEKUm5OamJGcDFXa1pTVXdwaVdHZzFWbTB4ZDFVeFdYZE5WbHBxVTBjNVRGVlVTalJo
TWsxNFZtNUtWbUpYZUZoV2ExWldaREZhYzFWcgpkRTVTTUZZMVZXMDFUMVpIUlhsVmJrWldZa1pL
ZGxaRldtRmpkM0JoQ2xKRlNtOVVWVkpYVTBaVmVXVkhkRnBXYXpWSVZqSTFRMVpXCldrWmpSbEpY
Vm14d2FGbDZSbUZXVmtwMFpFWmthVkp1UWtwV2JYaGhZakpGZUZkcmFGWlhSM2hSVld0a05GSlda
SFVLWWpOa1VGVlkKUWtWWGJtOTNUMVZPYmxCVU1Fc0sK
```

##解法

答えから言うと、Base64というエンコーディングを16回施されているらしい。
そんなん分かるかって感じだが、慣れてくるとアルファベットと数字(と'/'と'+')のみしか無いことからなんとなく予想が着くのだろうか。
あるいは有名なエンコーディングに対するデコードを試していくのだろうか。

とりあえず以下のような引数で与えた回数base64デコードを行うスクリプトを作成。

```b64.py
import sys
import base64

def decode(x):
    txt = 問題文
    for i in range(int(x)):
        txt = base64.b64decode(txt)

    return txt.decode()

argv = sys.argv
print(decode(argv[1]))
```

次にシェルスクリプトで1~16まで与えて実行してみる。

```loop.sh
#!/bin/sh
for((i=0;i<17;i++))
do
	echo "$i"
	python b64.py $i
done

```

結果

```
.
.
.

12
VjFjeFYySnRSbGhPUjJSUFlXeHJlVk5WVWpSaE1XeFpWVzFvVVZveU9IaFVWbFpUVld0c1ZWSnNX
bGxWTVdzeFZHdGFSMU5GTlVSVQpXRkpPWlcwNGQxTnNVbXBPVlRSM1lYcE9Ud3BSTVVwRFZteEdk
bG93VG5SV2JsWmhVVmM0T1VOblBUMEsK

13
V1cxV2JtRlhOR2RPYWxreVNVUjRhMWxZVW1oUVoyOHhUVlZTVWtsVVJsWllVMWsxVGtaR1NFNURU
WFJOZW04d1NsUmpOVTR3YXpOTwpRMUpDVmxGdlowTnRWblZhUVc4OUNnPT0K

14
WW1WbmFXNGdOalkySUR4a1lYUmhQZ28xTVVSUklURlZYU1k1TkZGSE5DTXRNem8wSlRjNU4wazNO
Q1JCVlFvZ0NtVnVaQW89Cg==

15
YmVnaW4gNjY2IDxkYXRhPgo1MURRITFVXSY5NFFHNCMtMzo0JTc5N0k3NCRBVQogCmVuZAo=

16
begin 666 <data>
51DQ!1U]&94QG4#-3:4%797I74$AU
 
end

```

16回目で意味のありそうな文字列となった。

begin 666 で調べてみると、[uuencode](https://ja.wikipedia.org/wiki/Uuencode)というものがあるらしい。

これをデコードするスクリプトを作成。
pythonの`uu`ライブラリを使うと、`uu.decode(読み出しファイル, 書き込みファイル)`でuuencodeのデコードができる。

```uudecode.py
import uu
uu.decode('crypt', 'flag.txt')

```

16回base64デコードした文字列を`crypt`というファイルに出力し、
先の`uuencode.py`を実行すると、flag.txtが作成される。

```
python b64.py 16 > crypt
python uuencode.py
```