Created
May 12, 2022 22:33
-
-
Save synap5e/a973ca814af89553a4b156476ab5b55a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from Crypto.Cipher import DES | |
from Crypto.Util.Padding import pad, unpad | |
from hashlib import md5 | |
IV = b'ABCDEFGH' | |
KEY = b'DES KEY!' | |
HASHSEP = b'=++!MD5!++=' | |
SECRET = b'SECRET123' | |
def encode(s: bytes) -> bytes: | |
cipher = DES.new(KEY, DES.MODE_CBC, IV) | |
payload = s + HASHSEP + md5(s).hexdigest().encode() | |
return cipher.encrypt(pad(payload, 8, style='pkcs7')) | |
token = encode(SECRET) | |
print(f'token=' + token.hex()) | |
cipher = DES.new(KEY, DES.MODE_CBC, IV) | |
token_dec = unpad(cipher.decrypt(token), 8, style='pkcs7') | |
print(f'+ {token_dec=}') | |
def decode(s: bytes) -> bool: | |
cipher = DES.new(KEY, DES.MODE_CBC, IV) | |
try: | |
dec = cipher.decrypt(s) | |
except Exception as e: | |
print(f'+ exception: {e}') | |
return False | |
print(f'+ {dec.hex()=}') | |
try: | |
up = unpad(dec, 8, style='pkcs7') | |
except Exception as e: | |
print(f'+ exception: {e}') | |
return False | |
if HASHSEP not in up: | |
print(f'+ no hashsep found') | |
return False | |
message, message_hash = up.split(HASHSEP, 1) | |
expected = md5(message).hexdigest().encode() | |
print(f'+ {message=} {message_hash=} {expected=}') | |
if expected != message_hash: | |
print(f'+ md5 mismatch') | |
return False | |
return True | |
while True: | |
s = input('> ').strip() | |
if s == SECRET.decode(): | |
print('You Win!') | |
break | |
try: | |
d = bytes.fromhex(s) | |
except Exception as e: | |
print(e) | |
else: | |
print(decode(d)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment