Path: blob/master/Message-Authentication-Code/CBC-MAC-Forgery/Challenges/He-Moron/server.py
1402 views
from Crypto.Cipher import AES1from Crypto.Util.strxor import strxor2from binascii import hexlify, unhexlify34def sign(key, message):5try:6ECB = AES.new(key, AES.MODE_ECB)7messageblocks = [message[i:i + 16] for i in range(0, len(message), 16)]8tag = ECB.encrypt(messageblocks[0])9for i in range(1,len(messageblocks)):10tag = ECB.encrypt(strxor(messageblocks[i], tag))11return hexlify(tag)12except:13print("\nYou can't sign that way! No padding done here boy!")14exit()1516if __name__ == '__main__':1718flag = "\nWhat? Nooooooooo!!! xiomara{1_b0w_d0wn_70_y0u!}"19key = b'YELLOW SUBMARINE'20print("\nYou wanna challenge me? You trying to break my signing scheme? LOLLLLLL ><")21print("Anyways, try hard for that boy!")22print("Press 0 to get your message signed and 1 to submit a forgery...Pffff! Seriously?")23while(True):24try:25inp = raw_input("\n")26if(inp=="0"):27hex_msg = raw_input("\nGimme your hex encoded message\n")28msg = unhexlify(hex_msg)29hex_tag = sign(key, msg)30print("\nThere you go! Here's my hex encoded tag!")31print(hex_tag)32else:33print("\nOh! So, you are up for it?")34print("\nAlright! Gimme just two different hex encoded messages that could sign to the same tag!")35msg1 = unhexlify(raw_input("\nMessage #1: \n"))36msg2 = unhexlify(raw_input("\nMessage #2: \n"))37if(msg1 == msg2):38print("\nI am not fool boy! Get back and do the job like a grown up!")39exit()40if(msg1 != msg2 and sign(key, msg1)==sign(key, msg2)):41print(flag)42exit()43else:44print("\nOops! They don't match! Told ya! Hard work my son...Better luck next time!")45exit()46except:47exit()484950