Skip to content

Instantly share code, notes, and snippets.

@rarecoil
Created April 22, 2020 06:23
Show Gist options
  • Save rarecoil/afcbcbf830fedc654043060d22424de6 to your computer and use it in GitHub Desktop.
Save rarecoil/afcbcbf830fedc654043060d22424de6 to your computer and use it in GitHub Desktop.
CryptoHack: Passwords as Keys
#!/usr/bin/env python3
from Crypto.Cipher import AES
import requests
import hashlib
import sys
import binascii
result = requests.get('https://aes.cryptohack.org/passwords_as_keys/encrypt_flag')
ciphertext_hex = result.json()["ciphertext"]
with open('words', 'r') as f:
for word in f:
word = word.strip()
attempted_key = hashlib.md5(word.encode()).hexdigest()
ciphertext = bytes.fromhex(ciphertext_hex)
key = bytes.fromhex(attempted_key)
cipher = AES.new(key, AES.MODE_ECB)
try:
decrypted = cipher.decrypt(ciphertext)
result = binascii.unhexlify(decrypted.hex())
if result.startswith('crypto{'.encode()):
print("key is %s" % word)
print(result.decode('utf-8'))
sys.exit(0)
except ValueError as e:
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment