main.py (1125B)
1#!/usr/bin/env pypy3 2 3import os 4from pydoc import plain 5from sys import byteorder 6from Crypto.Cipher import AES 7from Crypto.Util import Counter 8import hashlib 9 10# Create a secret.py file with a variable `FLAG` for local testing :) 11from secret import FLAG 12 13secret_key = os.urandom(16) 14 15def encrypt(plaintext, counter): 16 m = hashlib.sha256() 17 m.update(counter.to_bytes(8, byteorder="big")) 18 19 alg = AES.new(secret_key, AES.MODE_CTR, nonce=m.digest()[0:8]) 20 ciphertext = alg.encrypt(plaintext) 21 22 return ciphertext.hex() 23 24 25def main(): 26 print("DES is broken, long live the secure AES encryption!") 27 print("Give me a plaintext and I'll encrypt it a few times for you. For more security of course!") 28 29 try: 30 plaintext = bytes.fromhex(input("Enter some plaintext (hex): ")) 31 except ValueError: 32 print("Please enter a hex string next time.") 33 exit(0) 34 35 for i in range(0, 255): 36 print(f"Ciphertext {i:03d}: {encrypt(plaintext, i)}") 37 38 print("Flag:", encrypt(FLAG.encode("ascii"), int.from_bytes(os.urandom(1), byteorder="big"))) 39 40if __name__ == "__main__": 41 main()