42 lines
1.1 KiB
Python
Executable File
42 lines
1.1 KiB
Python
Executable File
#!/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(256):
|
|
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()
|