import base64, json, http_ece
from cryptography.hazmat.primitives.asymmetric import ec
def b64d(s): return base64.urlsafe_b64decode(s + "="*(-len(s)%4))
sub = json.load(open("sub.json"))
body = b64d(open("ct_deno.txt").read().strip())
priv = ec.derive_private_key(int.from_bytes(b64d(sub["ua_private"]),"big"), ec.SECP256R1())
pt = http_ece.decrypt(body, private_key=priv, auth_secret=b64d(sub["auth"]), version="aes128gcm")
print("decrypted:", pt.decode("utf-8"))
print("parsed JSON title:", json.loads(pt)["title"])
# negative control: flip one ciphertext byte
bad = bytearray(body); bad[-1] ^= 0x01
try:
    http_ece.decrypt(bytes(bad), private_key=priv, auth_secret=b64d(sub["auth"]), version="aes128gcm")
    print("NEGATIVE CONTROL FAILED — tampered ciphertext decrypted!")
except Exception as e:
    print("negative control OK — tamper rejected:", type(e).__name__, e)
# negative control 2: wrong auth secret
try:
    http_ece.decrypt(body, private_key=priv, auth_secret=b"0"*16, version="aes128gcm")
    print("NEGATIVE CONTROL 2 FAILED")
except Exception as e:
    print("negative control 2 OK — wrong auth rejected:", type(e).__name__)
