#conversation.py questions = [ b"Who are you?", b"Oh my dear Brooke, I miss you so much!", b"Brooke, I was so lonely during the outbreak of Novel coronavirus!", b"Brooke, do you mind me asking you a few more question?", b"What can I do for you?"] answer = [ b"I'm Brooke.", b"I miss you too.", b"Me too! I wanna go out for dance!", b"Not at all.", b"Nothing."] fake_answer=b"I want to get my AES-key." saying_key=b"Here is the key: "
from encryption import * from conversation import *
banner = """ +--------------------------------------------------+ |HELLO CHALLENGER! | | | |Have you ever seen movies like MISSION IMPOSSIBLE?| |Unfortunately, You have got a mission that seemed | |impossible to finish! | | | |In the task, you are required to decrypt the | |'Most secret' message of Arica, the bank clerk. | | | |Fortunately, after social engineering you have | |hacked in her communication with her boyfriend | | Brooke and you have got Brooke's partial | |RSA-private-key. | | | |To get the message, you must first establish | | communication with Arica through 'DH key exchange| | protocol', chat with her by saying what Brooke | |said, but you need to send the message | | "I want to get my AES-key." and the timestamp | | after Arica said "What can I do for you?", | |then she will give the AES-key encrypted with her | | RSA-public-key,this message should be start with| | "Here is the key:". | +--------------------------------------------------+ """
defmain(): print(banner) encrypt_flag() enc = encrypt_key() expA = getrandbits(128) expB = getrandbits(128) A = generateDH(expA) B = generateDH(expB) aricaSay("A:", A) AC = int(say2brooke("A: ")) brookeSay("B:", B) BC = int(say2arica("B: ")) keyC = pow(AC, expB, p) keyS = pow(BC, expA, p) keyC = long_to_bytes(keyC).rjust(16, b"\x00") keyS = long_to_bytes(keyS).rjust(16, b"\x00") cipherC = AES.new(keyC, AES.MODE_ECB) cipherS = AES.new(keyS, AES.MODE_ECB) for _i in range(5):#5 index=_i randomnum = randint(0, 256) messageA = questions[index] messageA = adaptmessage(messageA) cS = bytes_to_long(cipherS.encrypt(messageA)) aricaSay("A:", "{:032x}".format(cS)) s = int(say2brooke("A: "), 16) s = long_to_bytes(s) q = cipherC.decrypt(s) if q != messageA: print("Brooke does't get your words and realize something, U FAILED...") exit(0) randomnum = randint(0, 256) messageB = answer[index] messageB = adaptmessage(messageB) cC = bytes_to_long(cipherC.encrypt(messageB)) brookeSay("B:", "{:032x}".format(cC)) s = int(say2arica("B: "), 16) s = long_to_bytes(s) a = cipherS.decrypt(s) if(_i==4): messageB = fake_answer messageB = adaptmessage(messageB) if a != messageB: print("Arica does't get your words and realize something, U FAILED...") exit(0) randomnum = randint(0, 256) messageA = saying_key+long_to_bytes(enc) messageA = adaptmessage(messageA) cS = bytes_to_long(cipherS.encrypt(messageA)) aricaSay("A:", "{:032x}".format(cS))
#之所以要做出这个判断,是因为公钥真的不可能为0或负数 if(AC <=0): brookeSay("You are not Arica! Her public key should not be:", AC) exit(0) brookeSay("B:", B) BC = int(say2arica("B: ")) if(BC <=0): aricaSay("You are not Brooke! His public key should not be:", BC) exit(0)
+---------------------------------------------------------+ | PS: | | All message should be followed with new timestamp!!! | | newtimestamp = (oldtimestamp+1)%256 | | Please don't stuck with it:( | | For example, when you decrypt what Arica said like this:| | +--------------------+ | | | Brooke said: | | | +--------------------+ | | I'm Brooke. | | timestamp:112 | | You should encrypt this message to Arica: | | +---------------------+ | | | Say to Arica: | | | +---------------------+ | | I'm Brooke. | | timestamp:113 | +---------------------------------------------------------+
e=863 n = 100518394843898371534434468452366727877002363101196571557097132988734353726584916462381290904798733002716305256677572628142627545744681541293793069726231800754314572981168235829239563613754713724345581918566947492170647540708428013292098065584617876680109166595710006367308500341766024651457023537770009981295726110940010220082434490215229951191714998895277291076151298363584672162765486380515325199053994651202708865071379999942193926154807059329464291992287421875356468218685095369057734586073271903432589401520355996027480658214090147502829069822606753125953586123331154015992246394834867237734405963056883227552074835515815968441 c=86032824638305503499105979004374728344861493802341583733786447138684660694449673826205271902766363026345759436852747743967254218765160248630402258125202844303276381357264903281385470521070161164325276902001627351570675438339067652784355957225763207341000628151024518554862922220201109512940053316358078220681816224518400117972877854801506805929902506832781106133167532327833813179558969694268634413548545525533605595649634856393268111552838570383386406869870266457914799472663817890880697043874427042958587244568923338441053981863674366842769379075373006984522023718553749977766836594738859315852333132093474709715377682153430675891 d0=8429433927120666131169351623736961178529851960373209550653840828920672665447032524933438956407503522388445988812739437427277858013428300926608147855795349674824173869269407061090165202002106353884034590344852857623737262477675587193006629765892565228448265429521617733861051151345083787592170864896543517637252788111
d = 0 for k in range(0,e): print k X = var('X') results = solve_mod([e*d0*X - k*(n*X-X*X-n+X) == X], 2**1050) for x in results: p0 = ZZ(x[0]) if is_prime(p0) and gcd(n,p0)!=1: print p0 q0 = n//p0 phi=(q0-1)*(p0-1) d = inverse_mod(e,phi) break if(d!=0): break print d m = pow(c,d,n) print m
运行结果如图:
得到aeskey以后解密flag,脚本如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
from Cryptodome.Cipher import AES import base64 from Cryptodome.Util.number import ( bytes_to_long, long_to_bytes, ) key_in_num = 137504889148498708381160600835355727692 encryptedflag = b'gTAmtDzEDIYdzs6j55csresodxpsKJlOVMOmzLq8/39Vm0lJZvnrGtPBW6IKUpML' c = base64.b64decode(encryptedflag) key = long_to_bytes(key_in_num) print(key) aes = AES.new(key, AES.MODE_ECB) flag = aes.decrypt(c) print("Here is the decrypted flag: "+str(flag))