Skip to content

Instantly share code, notes, and snippets.

Created January 31, 2021 16:59
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
import os
import socketserver
import string
import threading
from time import *
import random
import time
import binascii
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
iv = b''
key = b''
flag = open("flag.txt", "rb").read().strip()
class Service(socketserver.BaseRequestHandler):
def handle(self):
assert len(flag) % 16 == 1
blocks = self.shuffle(flag)
ct = self.encrypt(blocks)
def byte_xor(self, ba1, ba2):
return bytes([_a ^ _b for _a, _b in zip(ba1, ba2)])
def encrypt(self, blocks):
curr = iv
ct = []
cipher =, AES.MODE_ECB)
for block in blocks:
curr = cipher.encrypt(curr)
ct.append(self.byte_xor(block, curr))
return b''.join(ct)
def shuffle(self, pt):
pt = pad(pt, 16)
pt = [pt[i: i + 16] for i in range(0, len(pt), 16)]
return pt
def send(self, string, newline=True):
if type(string) is str:
string = string.encode("utf-8")
if newline:
string = string + b"\n"
def receive(self, prompt="> "):
self.send(prompt, newline=False)
return self.request.recv(4096).strip()
class ThreadedService(
def main():
port = 3167
host = ""
service = Service
server = ThreadedService((host, port), service)
server.allow_reuse_address = True
server_thread = threading.Thread(target=server.serve_forever)
server_thread.daemon = True
print("Server started on " + str(server.server_address) + "!")
# Now let the main thread just wait...
while True:
if __name__ == "__main__":
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment