2021-07-04

【胖哈勃的七月公开赛】密码要先学好数学

一、题目描述

给了加密脚本和输出文本

from random import randint

a = 288260533169915
p = 1007621497415251
FLAG = b'flag{********************}'

def encrypt_flag(flag):
    ciphertext = []
    plaintext = ''.join([bin(i)[2:].zfill(8) for i in flag])
    print(plaintext)
    for b in plaintext:
        e = randint(1, p)
        n = pow(a, e, p)
        if b == '1':
            ciphertext.append(n)
        else:
            n = -n % p
            ciphertext.append(n)
    print(len(ciphertext))
    return ciphertext

print(encrypt_flag(FLAG))

二、解题思路

该题,其实是Pohlig-hellman(离散对数问题),很幸运前几天刚好了解了相关知识。

已知 a e mod p = n,其中给出了a、p、n的值,然后要求e(也不是真正的求e)。

根据题目意思,先把flag转化成二进制并填充,接着根据每位是“0”或者“1”进行下一步操作。

若当前位为”1“,则直接输出n,即a e mod p;若为”0“,则输出( - n % p)。

解这道题,我使用的是discrete_log()函数,若discrete_log(mod(c, p), mod(a, p))有解,则e存在,则当前位为”1“,否则为”0“。

代码如下:(运行时间较长,慢等)

#Sage
n = [835573322319412, 194156231145433, 459240835091427, 254691265527581, 452163501868176, 362769806018159, 390335330574588, 795683531947695, 547245847322642, 662157248109028, 973966064208336, 252129990307548, 578699863722488, 486222427902568, 117477413047771, 740421014593358, 863241886290392, 222275563655891, 293327716717603, 708319736688481, 889074509131674, 417808854196549, 131645986814206, 249411728529916, 500308738571101, 178419560186091, 441524245815824, 270385977926445, 134464725515031, 34977330760372, 789037598270288, 871556001954572, 42895524249924, 602041422192220, 45250560584859, 836594396205340, 154261010116600, 906631913900110, 73716306230031, 57491432561272, 937546091143710, 573584711515529, 117838942375694, 108224759885936, 686520256664437, 328396509053949, 964793767786949, 952055129356587, 570054348386776, 132721379375751, 421580578207025, 302791761439275, 558510385173476, 7941240282405, 10928401173457, 382447756114972, 169542347967514, 977453208629407, 460734109220968, 573505124794720, 318306022981107, 120648106475963, 807138639422627, 996507910024607, 102816788391640, 660745079166409, 809890775939749, 832519292688500, 87538065186141, 637100151940331, 382113003094066, 470655145986067, 644778242358267, 517586163010705, 558938708706131, 150978345005135, 102930326517355, 782154305431595, 6219189599369, 243324433725873, 650527665111501, 100280527101388, 1005535261333789, 987714102930385, 1002475950204796, 922079478085088, 906834270364060, 53211341275436, 965041750990423, 77115141669784, 588069693290238, 794822251044102, 930830807276545, 201161070608812, 568151623857765, 678968180937073, 412380912864953, 162493098979490, 631342883381540, 261680062360627, 227532833267531, 251016357185685, 827419020607751, 858376422971452, 362277983384639, 527363637534876, 334777651780889, 210527722411428, 808789791291861, 520556222865899, 704810558642854, 568037991932850, 169208358508287, 413145308937205, 599769322994671, 441328336467023, 849181035363926, 83477340978714, 80594200850041, 660277475107773, 928502236651086, 790965510818429, 135278692982626, 424887158380721, 411620739036446, 135280586082276, 714308921541654, 9109479300877, 650573139284244, 870861989942716, 443255252246208, 70455275832571, 369440682873276, 515899628421991, 612793429865648, 723589985084253, 160660351599873, 846726628855115, 8027417644170, 922790558110918, 329372648478795, 363470140166698, 86704784885484, 854835234890991, 976023205217544, 652708649867132, 388967328647036, 559512066466403, 36971809912721, 335187129410393, 6855900757516, 543488958018354, 287382478990521, 439225225775323, 661397580381098, 773933897777421, 523567536261705, 390598723653906, 417048954790947, 637431635583415, 335090251298424, 206308145752203, 501544720775707, 614789886972475, 357742401606467, 289036983452171, 668519107118940, 92139870679256, 388797793877078, 372191799798178, 1004628632761505, 145521166059580, 576232295004932, 119635574132509, 216801862623375, 155870719957478, 28321309890983, 732905921144578, 791832670754979, 671433181708904, 435663032194545, 316031621753570, 220369837362135, 356539134058642, 360074630241799, 764194424366054, 46729635512483, 924976743846057, 646158916629764, 894838984085320, 757222585897598, 829482878889900, 838347423444759, 590218802707709, 793621010210316, 79947449693367, 739723478650329, 163281924014926, 808389762984067, 208262587169431, 582578916561821, 478856137518673, 650032839378814, 58022200409911, 17291737478946, 919921371618469, 473933973533026, 861870666818810]
a = 288260533169915
p = 1007621497415251
t=''
for c in n:
    try:
        x=discrete_log(mod(c, p), mod(a, p))
        t+='1'
    except:
        t+='0'
print(t)
x=int('0b'+t,2)
print(x.to_bytes(26,'little'))#n的长度为208,所以flag长度为208//8=26

image-20210704204154894

最后得flag:flag{Patt3rns_1n_re5idu3s}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mxx307

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值