๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿด‍โ˜ ๏ธ CTF ๐Ÿด‍โ˜ ๏ธ/๐Ÿงฎ ์•”ํ˜ธํ•™ ๐Ÿงฎ

[Dream Hack - Crypto] ICM2022

๋ฐ˜์‘ํ˜•

ํ’€์ด ๊ณผ์ •

1. enc ํ•จ์ˆ˜์—์„œ q ๊ฐ’์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์‹์„ ๋งŒ์กฑํ•œ๋‹ค.

2. n = 3, key2 = 95 ์ผ๋•Œ, q = -200640142664324295933714 ์„ ๋งŒ์กฑํ•˜๋Š” p, key1 ์„ ๊ตฌํ•˜์ž!

3. key1 < key2 ์ด๋ฏ€๋กœ 0 ≤ key1 < 95 ์ด๊ณ , ์ž„์˜์˜ key1 ๊ฐ’์— ๋Œ€ํ•œ p ๊ฐ’์„ ํ™•์ธํ•˜์—ฌ flag๋ฅผ ์ถ”๋ก ํ•˜์ž!!

4. p ๊ฐ’์„ ํ™•์ธํ•˜๋Š” dec ํ•จ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์‹์„ ๋งŒ์กฑํ•œ๋‹ค.

 

5. dec ๋ฅผ ํ†ตํ•ด p ๋ฅผ ๊ตฌํ•˜๊ณ , enc ์— p ๋ฅผ ๋Œ€์ž…ํ•˜์—ฌ q_test ๊ตฌํ•œ ํ›„ ๊ธฐ์กด p ๊ฐ’๊ณผ ๋น„๊ตํ•œ๋‹ค.

 

ํ’€์ด ์ฝ”๋“œ

import random
from fractions import Fraction

# ์ฃผ์–ด์ง„ ๊ฐ’ p๋ฅผ n, key1, key2๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์•”ํ˜ธํ™”ํ•˜๋Š” ํ•จ์ˆ˜
def enc(p, n, key1, key2):
    q = (Fraction(p, n+1)*key1**(n+1)) - (Fraction(p, n+1)*key2**(n+1))
    print("[OK] plain is encrypted : ", q)
    return q

# ์•”ํ˜ธํ™”๋œ ๊ฒฐ๊ณผ q๋ฅผ ๋ณตํ˜ธํ™”ํ•˜๋Š” ํ•จ์ˆ˜ (์ฝ”๋“œ ๋‚ด์šฉ์ด ๋ˆ„๋ฝ๋˜์–ด ์žˆ์Œ)
def dec(q, n, key1, key2):
    temp = key1**(n+1) - key2**(n+1)
    p = int((n+1) * q / temp)
    print("[OK] cipher is decrypted : ", p)
    return p

# n, key1, key2๋ฅผ ๋žœ๋คํ•˜๊ฒŒ ์ƒ์„ฑ
n, key2 = 3, 95
q = -200640142664324295933714

# ๋ธŒ๋ฃจํŠธ ํฌ์Šค๋กœ ์˜๋ฏธ ์žˆ๋Š” ๋ฌธ์ž์—ด์ด ๋‚˜์˜ฌ ๋•Œ๊นŒ์ง€ ์‹คํ•ญ์„ ์‹œ์ผœ๋ณด์ž!!
for key in range(0,95) :
    p = dec(q, n, key, key2)
    print("key : ", key)
    q_test = enc(p, n, key, key2)
    if q_test == q :
        print("p : ", p)
        print("YEAH!!!!! here!!!!!")
        break

 

์‹คํ–‰ ๊ฒฐ๊ณผ

flag๋Š” DH{10112210997116104} ์ด๋‹ค. ์•„์Šคํ‚ค ์ฝ”๋“œ๋ฅผ ๋ณ€ํ™˜ํ•ด๋ณด๋‹ˆ ezmath๋ผ๊ณ  ํ•œ๋‹ค!!ใ…Žใ…Ž

๋ฐ˜์‘ํ˜•