0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Encoding Basics

Posted at

Encoding Basics

CTFのCryptoでよく使われるencode方法を学びましょう!

from Crypto.Util.number import *
import base64
import os

flag = os.getenv("FLAG", "DUMMYD{DUMMYDUMMYDUMMYDUMMYDUMMYDUMMYDUMMYDUMMYDUMMYDUMMYDUMMY}").encode()
flag1 = flag[:20]
flag2 = flag[20:40]
flag3 = flag[40:]

print(f"long_value = {bytes_to_long(flag1)}")
print(f'hex_string = "{flag2.hex()}"')
print(f'base64_string = "{base64.b64encode(flag3).decode()}"')

解法

これは、Cryptoでよく使われる表現です。
bytes_to_longの逆変換は、long_to_bytesを使えばできます。今回の場合は、次のようにdecodeできます。

long_to_bytes(flag1).decode()

次に、hex文字列は、bytes.fromhexを使ってバイト列に戻せます。今回の場合は、次のようにdecodeできます。

bytes.fromhex(flag2).decode()

最後に、base64でエンコードされているためbase64でdecodeします。

base64.b64decode(flag3).decode()

以上の手順で、すべてのフラグをdecodeできました。

from Crypto.Util.number import long_to_bytes
import base64

flag1 = 373502670300504551747111047082539140193958649718
flag2 = "346c5f6833785f6630726d61745f31735f636c33"
flag3 = "NG5fYjY0X3A0ZGQxbmdfaXNfY29vbH0="

flag = long_to_bytes(flag1).decode()
flag += bytes.fromhex(flag2).decode()
flag += base64.b64decode(flag3).decode()
print(flag)

Flag: Alpaca{b1g_1nt3ger_v4l_h3x_f0rmat_1s_cl34n_b64_p4dd1ng_is_cool}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?