diff options
| author | Louis Burda <quent.burda@gmail.com> | 2024-04-01 20:49:11 +0200 |
|---|---|---|
| committer | Louis Burda <quent.burda@gmail.com> | 2024-04-01 20:49:11 +0200 |
| commit | 4028d381f2ebbcaf9f3ca4a38062db7246471ab9 (patch) | |
| tree | 97201917b2fc3276cee07a271eb33298cf198671 /solve/main.py | |
| download | cscg2024-cry1-master.tar.gz cscg2024-cry1-master.zip | |
Diffstat (limited to 'solve/main.py')
| -rwxr-xr-x | solve/main.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/solve/main.py b/solve/main.py new file mode 100755 index 0000000..68e2417 --- /dev/null +++ b/solve/main.py @@ -0,0 +1,41 @@ +#!/usr/bin/env pypy3 + +import os +from pydoc import plain +from sys import byteorder +from Crypto.Cipher import AES +from Crypto.Util import Counter +import hashlib + +# Create a secret.py file with a variable `FLAG` for local testing :) +from secret import FLAG + +secret_key = os.urandom(16) + +def encrypt(plaintext, counter): + m = hashlib.sha256() + m.update(counter.to_bytes(8, byteorder="big")) + + alg = AES.new(secret_key, AES.MODE_CTR, nonce=m.digest()[0:8]) + ciphertext = alg.encrypt(plaintext) + + return ciphertext.hex() + + +def main(): + print("DES is broken, long live the secure AES encryption!") + print("Give me a plaintext and I'll encrypt it a few times for you. For more security of course!") + + try: + plaintext = bytes.fromhex(input("Enter some plaintext (hex): ")) + except ValueError: + print("Please enter a hex string next time.") + exit(0) + + for i in range(0, 255): + print(f"Ciphertext {i:03d}: {encrypt(plaintext, i)}") + + print("Flag:", encrypt(FLAG.encode("ascii"), int.from_bytes(os.urandom(1), byteorder="big"))) + +if __name__ == "__main__": + main() |
