1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| from Crypto.Cipher import AES from Crypto.Util.Padding import unpad import base64 import hashlib def decrypt_aes_ecb(ciphertext, key): cipher = AES.new(key.encode(), AES.MODE_ECB) decrypted_bytes = cipher.decrypt(base64.b64decode(ciphertext)) decrypted_text = unpad(decrypted_bytes, AES.block_size).decode('utf-8') return decrypted_text def read_from_file(file_path): with open(file_path, 'r') as file: return file.read() def read_key_from_file(key_path): with open(key_path, 'r') as key_file: keys = key_file.read().splitlines() return keys if __name__ == "__main__": ciphertext_path = "D:\\vscodework\\ctf\\5space\\cip.txt" key_path = "D:\\vscodework\\ctf\\5space\\pass.txt"
ciphertext = read_from_file(ciphertext_path) ciphertext = read_from_file(ciphertext_path) keys = read_key_from_file(key_path)
decryption_success = False for key in keys: try: mima = key.strip() key = hashlib.md5(mima.encode("utf-8")).hexdigest() key = key[0:16] decrypted_text = decrypt_aes_ecb(ciphertext, key) print(f"Decrypted Text with Key {key}:{mima}") print(decrypted_text) if "success" in decrypted_text: decryption_success = True print("Decryption successful. Stopping further attempts.") break break except Exception as e: print(f"Decryption with Key {key} failed: {str(e)}") if not decryption_success: print("Decryption unsuccessful with all keys.")
|