附件
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from Crypto.Util.number import *
import random
n = 2 ** 512
m = random.randint(2, n-1) | 1
c = pow(m, bytes_to_long(flag), n)
print 'm = ' + str(m)
print 'c = ' + str(c)
# m = 391190709124527428959489662565274039318305952172936859403855079581402770986890308469084735451207885386318986881041563704825943945069343345307381099559075
# c = 6665851394203214245856789450723658632520816791621796775909766895233000234023642878786025644953797995373211308485605397024123180085924117610802485972584499
分析
从附件的代码我们可以找到关键字:flag(c = pow(m, bytes_to_long(flag), n))
很明显这里的n不是质数,那么这道题就不是rsa了,查了一下这道题是离散对数问题
那么根据离散对数的原理
c = pow(m, bytes_to_long(flag), n)
mflag ≡ c (mod n)
求解离散对数可以使用python的sympy库,其中的discrete_log函数
已知条件:
n = 2 ** 512
m = 391190709124527428959489662565274039318305952172936859403855079581402770986890308469084735451207885386318986881041563704825943945069343345307381099559075
c = 6665851394203214245856789450723658632520816791621796775909766895233000234023642878786025644953797995373211308485605397024123180085924117610802485972584499
解题脚本:
from sympy.ntheory import discrete_log
import binascii
n = 2 ** 512
m = 391190709124527428959489662565274039318305952172936859403855079581402770986890308469084735451207885386318986881041563704825943945069343345307381099559075
c = 6665851394203214245856789450723658632520816791621796775909766895233000234023642878786025644953797995373211308485605397024123180085924117610802485972584499
flag_dec = discrete_log(n,c,m) #得到flag的十进制值
flag_hex=hex(flag_dec)
print(flag_hex)
print(binascii.a2b_hex(flag_hex[2:]))
拿到flag