워게임/CryptoHack

[CryptoHack] Mathmatics (Adrien's Signs)

  • -

Adrien's Signs

문제

Adrien은 심볼과 마이너스 기호를 이용하여 메시지를 암호화하는 방법을 고려하고 있습니다. 플래그를 복구하는 방법을 찾을 수 있을까요?

풀이

더보기

 

from random import randint

a = 288260533169915
p = 1007621497415251

FLAG = b'crypto{????????????????????}'


def encrypt_flag(flag):
    ciphertext = []
    plaintext = ''.join([bin(i)[2:].zfill(8) for i in flag])
    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)
    return ciphertext


print(encrypt_flag(FLAG))
output.txt

먼저 플래그 값을 받고, 이를 2진값으로 바꾼다.

받은 2진 값중 만약에 1이면 a^e mod p로 계산을하고

그게 아닌 0을 받게 되면 -.a^e mod p로 계산하고

계산된 값을 ciphertext에 append를하고

암호문으로 출력한다.

https://mokpo.tistory.com/634#:~:text=%EC%A6%89%2C%C2%A0%EC%96%B4%EB%96%A4%C2%A0%EC%A0%95%EC%88%98%C2%A0a%EA%B0%80%C2%A0%EC%A3%BC%EC%96%B4%EC%A7%80%EB%A9%B4%C2%A0pow(a%2C(p%2D1)//2%2Cp)%EB%A5%BC%C2%A0%EA%B3%84%EC%82%B0%ED%95%A8%EC%9C%BC%EB%A1%9C%EC%8D%A8%C2%A0a%EA%B0%80%C2%A0%EC%9D%B4%EC%B0%A8%EC%9E%89%EC%97%AC%EC%9D%B8%EC%A7%80%EB%A5%BC%C2%A0%EA%B2%B0%EC%A0%95%ED%95%A0%C2%A0%EC%88%98%C2%A0%EC%9E%88%EC%8A%B5%EB%8B%88%EB%8B%A4.

문제 이름인 adrien`s sign은 아드리앵의 기호로

아드리엥이 르장드르 기호를 만든 사람이기도한다.

 

그래서 혹시 르장드르 기호를 참고하여

먼저 a값이 p의 이차잉여인지 확인을 해보았다.

a = 288260533169915
p = 1007621497415251
print(bool(pow(a, (p-1)//2, p)==1))

이로써 a는 모듈러값 p의 이차잉여이다.

이때 -a^e mod p는 a^e mod p의 이차잉여의 반대인 이차비잉여이므로 마이너스값으로 곱해진걸 알수 있다.

 그래서 이차잉여, 이차비잉여인 숫자가 리스트에 있고, 이를 토대로 이진수를 복구하여 풀면

from Crypto.Util.number import *
a = 288260533169915
p = 1007621497415251
print(bool(pow(a, (p-1)//2, p)==1))

ciphertext = [67594220461269, 501237540280788, 718316769824518, 296304224247167, 48290626940198, 30829701196032, 521453693392074, 840985324383794, 770420008897119, 745131486581197, 729163531979577, 334563813238599, 289746215495432, 538664937794468, 894085795317163, 983410189487558, 863330928724430, 996272871140947, 352175210511707, 306237700811584, 631393408838583, 589243747914057, 538776819034934, 365364592128161, 454970171810424, 986711310037393, 657756453404881, 388329936724352, 90991447679370, 714742162831112, 62293519842555, 653941126489711, 448552658212336, 970169071154259, 339472870407614, 406225588145372, 205721593331090, 926225022409823, 904451547059845, 789074084078342, 886420071481685, 796827329208633, 433047156347276, 21271315846750, 719248860593631, 534059295222748, 879864647580512, 918055794962142, 635545050939893, 319549343320339, 93008646178282, 926080110625306, 385476640825005, 483740420173050, 866208659796189, 883359067574584, 913405110264883, 898864873510337, 208598541987988, 23412800024088, 911541450703474, 57446699305445, 513296484586451, 180356843554043, 756391301483653, 823695939808936, 452898981558365, 383286682802447, 381394258915860, 385482809649632, 357950424436020, 212891024562585, 906036654538589, 706766032862393, 500658491083279, 134746243085697, 240386541491998, 850341345692155, 826490944132718, 329513332018620, 41046816597282, 396581286424992, 488863267297267, 92023040998362, 529684488438507, 925328511390026, 524897846090435, 413156582909097, 840524616502482, 325719016994120, 402494835113608, 145033960690364, 43932113323388, 683561775499473, 434510534220939, 92584300328516, 763767269974656, 289837041593468, 11468527450938, 628247946152943, 8844724571683, 813851806959975, 72001988637120, 875394575395153, 70667866716476, 75304931994100, 226809172374264, 767059176444181, 45462007920789, 472607315695803, 325973946551448, 64200767729194, 534886246409921, 950408390792175, 492288777130394, 226746605380806, 944479111810431, 776057001143579, 658971626589122, 231918349590349, 699710172246548, 122457405264610, 643115611310737, 999072890586878, 203230862786955, 348112034218733, 240143417330886, 927148962961842, 661569511006072, 190334725550806, 763365444730995, 516228913786395, 846501182194443, 741210200995504, 511935604454925, 687689993302203, 631038090127480, 961606522916414, 138550017953034, 932105540686829, 215285284639233, 772628158955819, 496858298527292, 730971468815108, 896733219370353, 967083685727881, 607660822695530, 650953466617730, 133773994258132, 623283311953090, 436380836970128, 237114930094468, 115451711811481, 674593269112948, 140400921371770, 659335660634071, 536749311958781, 854645598266824, 303305169095255, 91430489108219, 573739385205188, 400604977158702, 728593782212529, 807432219147040, 893541884126828, 183964371201281, 422680633277230, 218817645778789, 313025293025224, 657253930848472, 747562211812373, 83456701182914, 470417289614736, 641146659305859, 468130225316006, 46960547227850, 875638267674897, 662661765336441, 186533085001285, 743250648436106, 451414956181714, 527954145201673, 922589993405001, 242119479617901, 865476357142231, 988987578447349, 430198555146088, 477890180119931, 844464003254807, 503374203275928, 775374254241792, 346653210679737, 789242808338116, 48503976498612, 604300186163323, 475930096252359, 860836853339514, 994513691290102, 591343659366796, 944852018048514, 82396968629164, 152776642436549, 916070996204621, 305574094667054, 981194179562189, 126174175810273, 55636640522694, 44670495393401, 74724541586529, 988608465654705, 870533906709633, 374564052429787, 486493568142979, 469485372072295, 221153171135022, 289713227465073, 952450431038075, 107298466441025, 938262809228861, 253919870663003, 835790485199226, 655456538877798, 595464842927075, 191621819564547]
plaintext = ''

for i in ciphertext:
    if bool(pow(i, (p-1)//2, p)==1):
        plaintext += '1'
    else :
        plaintext += '0'

plaintext = int(plaintext, 2)
print(long_to_bytes(plaintext))

플래그값은 crypto{p4tterns_1n_re5idu3s}이다.

 

Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.