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

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

[Dream Hack - Crypto] chinese what?

๋ฐ˜์‘ํ˜•

ํ’€์ด ๊ณผ์ •

  1. M ๊ณ„์‚ฐ: M์€ ๋ชจ๋“  pi (์—ฌ๊ธฐ์„œ๋Š” p1, p2, p3) ๊ฐ’์„ ๊ณฑํ•œ ๊ฐ’์ž…๋‹ˆ๋‹ค. M = p1 * p2 * p3
  2. mi ๊ณ„์‚ฐ: mi๋Š” M์„ ํ•ด๋‹น ์†Œ์ˆ˜ pi๋กœ ๋‚˜๋ˆˆ ๋ชซ์ž…๋‹ˆ๋‹ค. m1 = M // p1,  m2 = M // p2,  m3 = M // p3
  3. ์—ญ์› ๊ณ„์‚ฐ: mi์˜ ์—ญ์› yi๋Š” mi๋ฅผ ๋ชจ๋“ˆ๋กœ pi๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€์™€ ๊ณฑํ–ˆ์„ ๋•Œ 1์ด ๋˜๋Š” ๊ฐ’์ž…๋‹ˆ๋‹ค. ์—ญ์›์€ ๋ชจ๋“ˆ๋กœ ์—ฐ์‚ฐ์—์„œ ๋‚˜๋ˆ—์…ˆ์˜ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์—ญ์›์€ ํ™•์žฅ๋œ ์œ ํด๋ฆฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋“ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  4. ๋ณต์›: ์ด์ œ ์—ญ์›๊ณผ mi ๊ฐ’์„ ์‚ฌ์šฉํ•˜์—ฌ flag ๊ฐ’์„ ๋ณต์›ํ•ฉ๋‹ˆ๋‹ค. flag = (c1 * y1 * m1 + c2 * y2 * m2 + c3 * y3 * m3) % M

 

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

from Crypto.Util.number import long_to_bytes

p1 = 1527207470243143973741530105910986024271649986608148657294882537828034327858594844987775446712917007186537829119357070864918869
p2 = 2019864244456120206428956645997068464122219855220655920467990311571156191223237121636244541173449544034684177250532278907347407
p3 = 1801109020443617827324680638861937237596639325730371475055693399143628803572030079812427637295108153858392360647248339418361407
c1 = 232762450308730030838415167305062079887914561751502831059133765333100914083329837666753704309116795944107100966648563183291808
c2 = 869189375217585206857269997483379374418043159436598804873841035147176525138665409890054486560412505207030359232633223629185304
c3 = 1465704473460472286244828683610388110862719231828602162838215555887249333131331510519650513265133531691347657992103108331793683

def modInverse(a, m):
    m0 = m
    y = 0
    x = 1
    if (m == 1):
        return 0
    while (a > 1):
        q = a // m
        t = m
        
        m = a % m
        a = t
        t = y
        
        y = x - q * y
        x = t
    if (x < 0):
        x = x + m0
    return x

M = p1 * p2 * p3

m1 = M // p1
m2 = M // p2
m3 = M // p3

y1 = modInverse(m1, p1)
y2 = modInverse(m2, p2)
y3 = modInverse(m3, p3)

flag = (c1*y1*m1 + c2*y2*m2 + c3*y3*m3) % M
print(long_to_bytes(flag))

 

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

๋ฐ˜์‘ํ˜•