#!/usr/bin/env python3

import sys
import base64
from Crypto.Cipher import AES
from errors import *


def main():
	try:
		file = checkFile(7)
		key = bytes.fromhex(file.readline().rstrip("\r\n"))
		text = (base64.b64decode(file.readline().rstrip("\r\n")))
		
		if (not(key) or not(text)):
			raise ValueError
		decrypt(text, key)
	except ValueError:
		fileError()

def decrypt(text, key):
	cipher = AES.new(key, AES.MODE_ECB)
	print(encode64(unpad((cipher.decrypt(text)))))

b64_index_table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

def encode64(input):
	output = ""

	bits = list("".join([bin(byte)[2:].zfill(8) for byte in input]))
	blocks = [bits[i:i+6] for i in range(0, len(bits), 6)]

	for block in blocks:
		block = "".join(block)

		if len(block) == 2:
			if "1" in block:
				block += "0000"
				output += b64_index_table[int(block, 2)] + "=="
			else:
				output += "=="
		elif len(block) == 4:
			if "1" in block:
				block += "00"
				output += b64_index_table[int(block, 2)] + "="
			else:
				output += "="
		elif len(block) == 6:
			output += b64_index_table[int(block, 2)]
	return output


def pad(input, size):
	if len(input) == size:
		return input
	padding_length = size - len(input) % size
	padding = bytes([padding_length] * padding_length)
	return (input + padding)

def unpad(input):
	padding = input[-input[-1]:]
	if not all(padding[byte] == len(padding) for byte in range(0, len(padding))):
		return input
	return input[:-input[-1]]

if __name__ == "__main__":
	main()