Masalah
Xmen OR the avengers (100pts)
The legion of doom is expecting an impending attack from a group of superheroes. they are not sure if it is the Xmen OR the avengers. they have received some information from a spy, a zip file containing the following files:
info_crypt.txt
info_clear.txt
superheroes_group_info_crypt.txt
help the legion of doom in decrypting the last file so they can prepare themselves and prevent their impending doom.
Penyelesaian
Masalah diatas dapat diselesaikan dengan XOR dan AES-ECB.
from Crypto.Util.strxor import strxor
import re
from hashlib import md5
from base64 import b64decode, b64encode
from Crypto.Cipher import AES
# Padding for the input string --not
# related to encryption itself.
BLOCK_SIZE = 16 # Bytes
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * \
chr(BLOCK_SIZE - len(s) % BLOCK_SIZE)
unpad = lambda s: s[:-ord(s[len(s) - 1:])]
class AESCipher:
"""
Usage:
c = AESCipher('password').encrypt('message')
m = AESCipher('password').decrypt(c)
Tested under Python 3 and PyCrypto 2.6.1.
"""
def __init__(self, key):
self.key = md5(key).hexdigest()
# self.key = key
def encrypt(self, raw):
raw = pad(raw)
cipher = AES.new(self.key, AES.MODE_ECB)
return b64encode(cipher.encrypt(raw))
def decrypt(self, enc):
enc = b64decode(enc)
cipher = AES.new(self.key, AES.MODE_ECB)
return cipher.decrypt(enc)
key = strxor(open('info_clear.txt').read(), open('info_crypt.txt').read()).rstrip()
# print key
# print len(key)
# key = key.rstrip()
# print len(key)
cipher = open('superheroes_group_info_crypt.txt').read().rstrip()
# password = ''.join(re.findall(r'([A-Z]){1,2}', key))
# print password
# password = ' 12345'
# password = '12345'
# password = 12345
# password = 'IV'
password = key
# password = password.lower()
# password = 'I Vouch for this: 12345'
# password = 'this'
# password = 'ISTHEKEYIV'
# password = 'THEKEYIV'
# password = 'KEYIV'
# password = 'IV'
# password = password.encode('base64')
# print len(password)
# ptx = 'YELLOW SUBMARINE'
# ctx = AESCipher(password).encrypt(ptx)
# assert ptx == AESCipher(password).decrypt(ctx)
print AESCipher(password).decrypt(cipher)
# print AES.new(password, AES.MODE_ECB).decrypt(cipher.decode('base64'))